Generic Inference

Pro Members Only

This lesson is available if you have an active subscription.

Alternatively, some member might be able to gift it to you.

If you already have a subscription, you can sign in.

Generic Inference

Subscription Required

You must have an active subscription to access this content.
If you already have a subscription, you can sign in.

Basic

function identity<T>(value: T): T {
return value;
}

let specified = identity<string>('hello');
// ^?

let inferred = identity('hello');
// ^?

With const

function identity<T>(value: T): T {
return value;
}

let inferred = identity('hello');
// ^?

const inferredConst = identity('hello');
// ^?

With as const

function free<T>(a: T, b: T) {
return [a, b];
}

const freeRes = free(1, 2);
// ^?

function explicit<T>(a: T, b: T): [T, T] {
return [a, b];
}

const explicitRes = explicit(1, 2);
// ^?

function guided<T>(a: T, b: T) {
return [a, b] as const;
}

const guideRes = guided(1, 2);
// ^?
javascript
typescript
react
playwright

Enjoy free content straight from your inbox 💌

No spam, unsubscribe at any time.

Transcript

00:00

Generics are a really powerful feature of TypeScript. And one cool thing about generics is that the compiler will try to infer the type as much as possible. Let's take a look at a few examples. Consider a simple generic identity function, which takes a generic argument of type T, takes a parameter of type T and then returns a type T. Now, of course, when we use this function, we can explicitly pass in a generic type argument. For example, here we are passing in the type string and that is what will be used any place. The Type T appears in the function, so as an example, the output will be of type string.

00:32

Now, we could just as easily let type script infer the Type T based on the past in argument. Now here, because we are passing in a string value is inferred to be of type string, and therefore the return type is also inferred as a string. There are a number of other factors that play into the inference engine as well. For example, if we declare the variable as a cons, instead of a let type strip, will see that the return type should be a cons. And then similar to how a string will be inferred as a literal, if you use a cons, the generic type is going to be the literal hello instead of just a plain string. Another simple way in which we can guide types script's

01:06

generic inference is with the as cons operator, let's create a generic function that takes two arguments of type T and then returns an array containing two items, A and B. Right now we haven't specified the return type, so if we invoke the function with two numbers, TypeScript infers the return type to be a number array. We can, however, choose to be a bit more explicit about our return type and specify it manually, that we want it to be a double consisting of two item instead of just a T array. And now when we invoke the function with two numbers, we get back a double of number and number.

01:40

Now instead of having to manually type the return type, we could help the inference engine along by using the as cons operator. And now when we invoke the guided version of this function, we automatically get back a double instead of having to specify it explicitly. Notice that the return type is also inferred as read-only because that is what the operator does. It chooses the as strict type as possible and marks the double As read only, so it cannot be modified.