TypeScript · Middle · Question 48 of 75

How do you make a generic parameter optional with a default value?

Explanation

Type parameters can have defaults: Box without an argument is Box<string>. They can be combined with constraints: <T extends object = {}>. That's how, for example, useState<S = undefined> and EventEmitter<T = any> work.

Further reading

Problem with a question

TypeScript · Middle · question 48

Test yourself

Take the TypeScript · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz