What does the syntax <T, U = T> mean in typescript?

I am familiar with the syntax <T, U> and its purpose but I am a bit confused with the syntax <T, U = T> and I can’t find it in the typescript documentation. Please recommend reading material. Thanks.

I have tried reading the typescript documentation but I can’t seem to find any mention of <T, U = T>

>Solution :

It provides a default value for the U type which, in this case, it is the T type.

Take a look at this example:

// This is our good old generic type. It needs to be provided or it will be inferred if possible
function myFunc<T>() {}

// Same as before but, it will use `string` type for `T` unless it is provided
function myFunc<T = string>() {}

// Same as the first example with 2 type parameters
function myFunc<T, U>() {}

// Also 2 type parameters but the second one will be `string` if not provided
function myFunc<T, U = string>() {}

// Also 2 type parameters but the second one will be `T` if not provided
function myFunc<T, U = T>() {}

Leave a Reply