TypeScript · Middle · Question 48 of 75
How do you make a generic parameter optional with a default value?
- ✓Correct answer:
type Box<T = string> = { value: T } - 2
type Box<T?> = { value: T } - 3
type Box<T | string> = { value: T }
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.