Type Inference in TypeScript

TypeScript can infer (and then check) the type of a variable based on a few simple rules. Because these rules
are simple you can train your brain to recognize safe / unsafe code (it happened for me and my team mates quite quickly).

The types flowing is just how I imagine in my brain the flow of type information.

Definition

Types of a variable are inferred by definition.

  1. let foo = 123; // foo is a `number`
  2. let bar = "Hello"; // bar is a `string`
  3. foo = bar; // Error: cannot assign `string` to a `number`

This is an example of types flowing from right to left.

Return

The return type is inferred by the return statements e.g. the following function is inferred to return a number.

  1. function add(a: number, b: number) {
  2. return a + b;
  3. }

This is an example of types flowing bottom out.

Assignment

The type of the function parameters / return can also be inferred by assignment e.g. here we say that foo is an Adder, that makes the type of a and b to infer as number.

  1. type Adder = (a: number, b: number) => number;
  2. let foo: Adder = (a, b) => a + b;

This fact can be demonstrated by the below code which raises an error as you would hope:

  1. type Adder = (a: number, b: number) => number;
  2. let foo: Adder = (a, b) => {
  3. a = "hello"; // Error: cannot assign `string` to a `number`
  4. return a + b;
  5. }

This is an example of types flowing from left to right.

The same assignment style type inference works if you create a function for a callback argument. After all an argument -> parameteris just another form of variable assignment.

  1. type Adder = (a: number, b: number) => number;
  2. function iTakeAnAdder(adder: Adder) {
  3. return adder(1, 2);
  4. }
  5. iTakeAnAdder((a, b) => {
  6. // a = "hello"; // Would Error: cannot assign `string` to a `number`
  7. return a + b;
  8. })

Structuring

These simple rules also work in the presence of structuring (object literal creation). For example in the following case the type of foo is inferred to be {a:number, b:number}

  1. let foo = {
  2. a: 123,
  3. b: 456
  4. };
  5. // foo.a = "hello"; // Would Error: cannot assign `string` to a `number`

Similarly for arrays:

  1. const bar = [1,2,3];
  2. // bar[0] = "hello"; // Would error: cannot assign `string` to a `number`

And of course any nesting:

  1. let foo = {
  2. bar: [1, 3, 4]
  3. };
  4. foo.bar[0] = 'hello'; // Would error: cannot assign `string` to a `number`

Destructuring

And of course, they also work with destructuring, both objects:

  1. let foo = {
  2. a: 123,
  3. b: 456
  4. };
  5. let {a} = foo;
  6. // a = "hello"; // Would Error: cannot assign `string` to a `number`

and arrays:

  1. const bar = [1, 2];
  2. let [a, b] = bar;
  3. // a = "hello"; // Would Error: cannot assign `string` to a `number`

And if the function parameter can be inferred, so can its destructured properties. For example here we destructure the argument into its a/b members.

  1. type Adder = (numbers: { a: number, b: number }) => number;
  2. function iTakeAnAdder(adder: Adder) {
  3. return adder({ a: 1, b: 2 });
  4. }
  5. iTakeAnAdder(({a, b}) => { // Types of `a` and `b` are inferred
  6. // a = "hello"; // Would Error: cannot assign `string` to a `number`
  7. return a + b;
  8. })

Type Guards

We have already seen how Type Guards help change and narrow down types (particularly in the case of unions). Type guards are just another form of type inference for a variable in a block.

Warnings

Be careful around parameters

Types do not flow into the function parameters if it cannot be inferred from an assignment. For example in the following case the compiler does not know the type of foo so it cannot infer the type of a or b.

  1. const foo = (a,b) => { /* do something */ };

However if foo was typed the function parameters type can be inferred (a,b are both inferred to be of type number in the example below).

  1. type TwoNumberFunction = (a: number, b: number) => void;
  2. const foo: TwoNumberFunction = (a, b) => { /* do something */ };

Be careful around return

Although TypeScript can generally infer the return type of a function, it might not be what you expect. For example here function foo has a return type of any.

  1. function foo(a: number, b: number) {
  2. return a + addOne(b);
  3. }
  4. // Some external function in a library someone wrote in JavaScript
  5. function addOne(a) {
  6. return a + 1;
  7. }

This is because the return type is impacted by the poor type definition for addOne (a is any so the return of addOne is any so the return of foo is any).

I find it simplest to always be explicit about function / returns. After all these annotations are a theorem and the function body is the proof.

There are other cases that one can imagine, but the good news is that there is a compiler flag that can help catch such bugs.

noImplicitAny

The flag noImplicitAny instructs the compiler to raise an error if it cannot infer the type of a variable (and therefore can only have it as an implicit any type). You can then

  • either say that yes I want it to be of type any by explicitly adding an : any type annotation
  • help the compiler out by adding a few more correct annotations.