Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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>

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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>() {}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading