How TypeScript breaks Referential Transparency
Tags: typescript, programming, javascript, typesafety
Published: 2019-07-08 17:59:04

---
title: How TypeScript breaks Referential Transparency
published: true
tags: typescript,programming,javascript,type-safety
canonical_url: https://blog.logrocket.com/how-typescript-breaks-referential-transparency-7a82c0345f2c
---

#### **(and what to do about it)**

![](https://cdn-images-1.medium.com/max/1024/1*KTh3puTlJIF6vAuUUu_LAQ.jpeg)

Have you ever factored out a constant in TypeScript, only to have the type checker start reporting some mysterious errors? There’s a fundamental reason for this. In this post, I’ll show a few examples of the problem, explain why it happens and present a few possible workarounds.

[![](https://cdn-images-1.medium.com/max/1024/1*GPjaPKNNUYHU8EsA3Z0JGA.png)](https://logrocket.com/signup/)

### Referential Transparency in JavaScript

Suppose we have a function, purchase, which buys a product for us:
```jsx
function purchase(product) { /* ... */ }
purchase('Macbook Air');
```
We’ll call this “inline form.” In JavaScript, you can factor the inline parameter out into a variable and pass a reference without changing the behavior of your code:
```jsx
const product = 'Macbook Air';
purchase(product);  // reference form
```
This is sometimes called Referential Transparency or the [Substitution Principle](http://www.sicpdistilled.com/section/1.1.5/). It refers to the idea that you can replace an expression with its value without changing behavior. This goes hand-in-hand with code being free of side effects and facilitates reasoning about program correctness.

So long as you don’t change the order of evaluation, factoring out a variable this way in JavaScript should not change the result of an expression.

### Referential Transparency in TypeScript

Factoring out a variable works in JavaScript, but what about TypeScript?
```jsx
export function purchase(what: string) { /* ... */ }

purchase('Macbook Air');  // Inline

const product = 'Macbook Air';
purchase(product);  // reference
```
So far so good!

Now let’s make it a little more complicated by pretending we’re working with a map visualization which lets us programmatically pan the map:
```jsx
// Parameter is a (latitude, longitude) pair.
function panTo(where: [number, number]) { /* ... */ }

// Inline:
panTo([10, 20]);  // inline: ok

// Reference:
const loc = [10, 20];
panTo(loc);  // reference. Will this work?
```
If you run this in the [TypeScript playground](http://www.typescriptlang.org/play/#src=export%20function%20panTo(where%3A%20%5Bnumber%2C%20number%5D)%20%7B%20%7D%0D%0A%0D%0Aconst%20loc%20%3D%20%5B10%2C%2020%5D%3B%0D%0ApanTo(loc)%3B%0D%0A), you’ll get an error:

![](https://cdn-images-1.medium.com/max/1024/0*lLuI-Bz-o5n4oKb-)

The relevant part is this:

```jsx
Argument of type ‘number[]’ is not assignable to parameter of type '[number, number]'.
```

You can mouse over the loc symbol in the [TypeScript playground](http://www.typescriptlang.org/play/#src=export%20function%20panTo(where%3A%20%5Bnumber%2C%20number%5D)%20%7B%20%7D%0D%0A%0D%0Aconst%20loc%20%3D%20%5B10%2C%2020%5D%3B%0D%0ApanTo(loc)%3B%0D%0A) to get a clearer sense of what’s going on:

![](https://cdn-images-1.medium.com/max/438/0*oAbjC3RK0qk3XRWn)

TypeScript has inferred the type of loc as number[], i.e. an array of numbers with an unknown length. When this is passed to panTo, TypeScript checks whether number[] (an array of numbers) is assignable to [number, number] (a pair of numbers). And it isn’t because some arrays don’t have two elements. Hence the error.

Why does TypeScript infer the type as number[] instead of [number, number]? Because this is valid JavaScript:
```jsx
const loc = [10, 20];
loc.push(30);
```
In other words, const in JavaScript is not deep. You can’t reassign loc but you can modify it. Some languages (e.g. C++) let you model the precise depth of const-ness, but JavaScript and TypeScript [do not](https://github.com/Microsoft/TypeScript/issues/26979).

So how do you fix this? An easy way is to throw any types into your code, to effectively silence the warning:
```jsx
const loc: any = [10, 20];
panTo(loc);
const loc = [10, 20];
panTo(loc as any);
```
If you’re going to do this, the latter form is preferable since the any is scoped to the function call and won’t prevent type-checking other uses of loc elsewhere in your code.

A better solution is to tell TypeScript precisely what you mean:
```jsx
const loc: [number, number] = [10, 20];
panTo(loc);
```
This is a bit verbose, but it’s the best solution in terms of type safety.

### Inference and string types

The previous example showed that Referential Transparency can break with tuple types. But that’s not the only situation in which this can come up.

Let’s revisit the purchase example from the start of the post. Let’s refine the type of products that you’re allowed to purchase using a union of string literal types:
```jsx
type Product = 'iPad' | 'Mac Mini' | 'Macbook Air' | 'Macbook Pro';
export function purchase(what: Product) {
 // mine bitcoin…
 // send to apple…
}
```
Purchasing inline expressions and purchasing by reference both work:
```jsx
purchase('Macbook Air');
const newToy = 'Macbook Air';
purchase(newToy);  // ok
```
If we use let instead of const, however, TypeScript complains:

![](https://cdn-images-1.medium.com/max/1024/0*FVK6fNBJZHHsNDSj)

The error is that `Argument of type ‘string’ is not assignable to parameter of type ‘Product’`. Using let tells TypeScript that we want to allow newToy to be reassigned. TypeScript doesn’t know the domain of possible values that we want to allow for newToy, so it guesses string. When we use const, it cannot be reassigned and so TypeScript can infer the narrower string literal type "Macbook Air".

As before, the solution is to tell TypeScript what we mean:
```jsx
let newToy: Product = 'Macbook Air';
purchase(newToy); // ok
```
Because const is shallow, it’s not a panacea for this sort of issue. It can still arise when types are inferred for properties in an object. For example:

![](https://cdn-images-1.medium.com/max/1024/0*bpXKXVNNbnPSE4KO)

As before, the error is that `'string' is not assignable to type 'Product'`. This is because what’s type is inferred as string to allow this sort of pattern:
```jsx
const order = {
  what: 'Macbook Air',
  how: 'bitcoin'
};
order.what = 'Lenovo'; // valid
```
As before, the solution is to tell TypeScript what you mean. One way is with a typecast:
```jsx
const order = {
  what: 'Macbook Air' as Product, // or "as 'MacbookAir'"
  how: 'bitcoin'
};
purchase(order.what); // ok
```
Another is by defining an Order interface which specifies the precise types of its properties:
```jsx
interface Order {
  what: Product;
  how: 'bitcoin' | 'usd';
}
const order: Order = {
  what: 'Macbook Air',
  how: 'bitcoin'
};
purchase(order.what); // ok
```
If you get an error like this involving a call to a third-party library, your best bet is to find the relevant type in its type declarations. Let’s look at a real-world example of that.

### Inference and React

Here’s some React code which renders a [Mapbox GL](https://docs.mapbox.com/mapbox-gl-js/api/) map using the [react-mapbox-gl](https://github.com/alex3165/react-mapbox-gl/) wrapper:
```jsx
import * as React from 'react';
import ReactMapboxGl from 'react-mapbox-gl';

const Map = ReactMapboxGl({
  accessToken: '....',
});

export function render(): JSX.Element {
  return (
    <Map
      style="mapbox://styles/mapbox/streets-v9"
      center={[-73.991284, 40.741263]}
      zoom={[14.5]}
      pitch={[45]}
      bearing={[-17.6]}
    >
        { /* ... */ }
    </Map>
  );
}
```
If we factor the initial viewport out into a constant, we get a long error:

![](https://cdn-images-1.medium.com/max/1024/0*534CmZ9gjroI_fBR)

The relevant bit is this:

```jsx
Types of property 'center' are incompatible.
 Type 'number[]' is missing the following properties from type '[number, number]': 0, 1
```

The center prop is expected to be [number, number] (a pair of numbers), but TypeScript has inferred the type of `INIT_VIEW.center` as number[] (an array). So this is really the same problem as before! And as before, the best solution is to specify a type for `INIT_VIEW`.

But if you look through the [typings for react-mapbox-gl](https://github.com/alex3165/react-mapbox-gl/blob/005a7a1d903dd0f7d64035bfc3d3cd30fdafbe7e/src/map.tsx#L45-L61), the closest thing is the Props structure, which contains all the properties in `INIT_VIEW` and then some:
```jsx
export interface Props {
  style: string | MapboxGl.Style;
  center?: [number, number];
  zoom?: [number];
  maxBounds?: MapboxGl.LngLatBounds | FitBounds;
  fitBounds?: FitBounds;
  fitBoundsOptions?: FitBoundsOptions;
  bearing?: [number];
  pitch?: [number];
  containerStyle?: React.CSSProperties;
  className?: string;
  movingMethod?: 'jumpTo' | 'easeTo' | 'flyTo';
  animationOptions?: Partial<AnimationOptions>;
  flyToOptions?: Partial<FlyToOptions>;
  children?: JSX.Element | JSX.Element[] | Array<JSX.Element | undefined>;
}
```
So we can’t just write `const INIT_VIEW: Props` to fix the problem because our constant doesn’t have the required style property. So we need to adapt the type. The simplest way is to use [Partial](https://netbasal.com/getting-to-know-the-partial-type-in-typescript-ecfcfbc87cb6), which makes all the properties in a type optional:
```jsx
const INIT_VIEW: Partial<Props> = {
  center: [-73.991284, 40.741263],
  zoom: [14.5],
  pitch: [45],
  bearing: [-17.6],
};

export function render(): JSX.Element {
  return (
    <Map
      style="mapbox://styles/mapbox/streets-v9"
      {...INIT_VIEW}  // ok
    >
        { /* ... */ }
    </Map>
  );
}
```
The Partial trick works so long as all the properties you want to factor out are optional. If we also wanted to factor out style (which is required), then we’d get an error: Property 'style' is optional in type …. By using Partial, we’ve told TypeScript to forget that we’ve explicitly set style.

One solution is to use [Pick](https://dev.to/treyhuffine/advanced-typescript-types-with-examples-4ppn-temp-slug-3571917) to pull out just the properties of interest along with their types. Unlike Partial, this will track which properties we’ve set and which we haven’t:
```jsx
const INIT_VIEW: Pick<MapProps, 'center'|'zoom'|'pitch'|'bearing'|'style'> = {
  center: [-73.991284, 40.741263],
  zoom: [14.5],
  pitch: [45],
  bearing: [-17.6],
  style: "mapbox://styles/mapbox/streets-v9"
};
```
It’s unfortunate that we have to repeat the keys in the type. Fortunately, you can use this [one weird trick](https://stackoverflow.com/a/46101222/388951) from StackOverflow to define a helper to infer the keys for you:
```jsx
const inferPick = <V extends {}>() => <K extends keyof V>(x: Pick<V, K>): Pick<V, K> => x;

const INIT_VIEW = inferPick<MapProps>()({
  center: [-73.991284, 40.741263],
  zoom: [14.5],
  pitch: [45],
  bearing: [-17.6],
  style: "mapbox://styles/mapbox/streets-v9"
});
```
The wrapper is unfortunate but the lack of repetition makes this a better solution. Any minifier should be able to remove the function call.

If you have a few possibilities for the constant (ala [StyleSheet.create in React Native](https://facebook.github.io/react-native/docs/style)) then the situation is a little trickier. You could try an index type:

```jsx
const views: {[viewName: string]: Partial<Props>} = {
 nyc: …,
 sf: …
};
```

This will work, but it would prevent TypeScript from flagging invalid references like views.london. You can solve this with a similar trick to the previous one:
```jsx
const inferKeys = <V extends {}>() => <K extends string>(x: Record<K,V>): Record<K,V> => x;

const INIT_VIEW = inferKeys<Partial<MapProps>>()({
  nyc: {
    center: [-73.991284, 40.741263],
    zoom: [14.5],
    pitch: [45],
    bearing: [-17.6],
  },
  sf: {
    center: [-73.991284, 40.741263],
    zoom: [14.5],
    pitch: [45],
    bearing: [-17.6],
  }
});

export function render(): JSX.Element {
  return (
    <Map
      style="mapbox://styles/mapbox/streets-v9"
      {...INIT_VIEW.nyc}  // ok
    >
        { /* ... */ }
    </Map>
  );
}
```
### Conclusions

When you factor out a constant and run into a confusing TypeScript error, you may be running into an [issue](https://github.com/Microsoft/TypeScript/issues/27502) around type inference and referential transparency. As we’ve seen in this post, the solution is usually to find or create a type that you can use in the assignment. Sometimes this is easy and sometimes it is not.

This is a pain point in learning TypeScript because it produces complex errors involving advanced elements of TypeScript’s type system for a simple, valid code transformation. The TypeScript team has a history of coming up with clever solutions to problems like these and they’re [actively discussing](https://github.com/Microsoft/TypeScript/issues/27486) this one. So stay tuned!
* * *
### Plug: [LogRocket](https://logrocket.com/signup/), a DVR for web apps

[![](https://cdn-images-1.medium.com/max/1024/1*s_rMyo6NbrAsP-XtvBaXFg.png)](https://logrocket.com/signup/)<figcaption><a href="https://logrocket.com/signup/">https://logrocket.com/signup/</a></figcaption>

[LogRocket](https://logrocket.com/signup/) is a frontend logging tool that lets you replay problems as if they happened in your own browser. Instead of guessing why errors happen, or asking users for screenshots and log dumps, LogRocket lets you replay the session to quickly understand what went wrong. It works perfectly with any app, regardless of framework, and has plugins to log additional context from Redux, Vuex, and @ngrx/store.

In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with headers + bodies, browser metadata, and custom logs. It also instruments the DOM to record the HTML and CSS on the page, recreating pixel-perfect videos of even the most complex single page apps.

[Try it for free.](https://logrocket.com/signup/)
* * *
The post [How TypeScript breaks referential transparency](https://blog.logrocket.com/how-typescript-breaks-referential-transparency-7a82c0345f2c/) appeared first on [LogRocket Blog](https://blog.logrocket.com).