Recall & Review
beginner
What are default generic types in TypeScript?
Default generic types are preset types given to generic parameters so that if no type is provided when using the generic, the default is used automatically.
Click to reveal answer
beginner
How do you specify a default generic type in TypeScript?
You specify a default generic type by using an equals sign after the generic parameter name, like <T = string>.
Click to reveal answer
beginner
Consider this code: <br>
function example<T = number>(value: T): T { return value; }<br>What happens if you call example() without specifying a type?The function uses the default generic type number, so TypeScript treats value as a number.
Click to reveal answer
beginner
Why are default generic types useful?
They make generics easier to use by providing a fallback type, so users don’t always have to specify the type explicitly.
Click to reveal answer
beginner
Can you override a default generic type when calling a generic function?
Yes, you can provide a specific type when calling the function, which replaces the default generic type.
Click to reveal answer
What does this syntax mean in TypeScript?
function foo() {}✗ Incorrect
The syntax means T is a generic type parameter with a default type of string.
If a generic function has a default type, what happens when you call it without specifying a type?
✗ Incorrect
When no type is specified, the default generic type is used automatically.
How do you override a default generic type when calling a generic function?
✗ Incorrect
You override the default by specifying the type explicitly, like foo().
Which of these is a benefit of default generic types?
✗ Incorrect
Default generic types make generics easier to use by providing a fallback type.
What happens if you specify a generic type when a default is already set?
✗ Incorrect
The explicitly provided type overrides the default generic type.
Explain what default generic types are and why they are useful in TypeScript.
Think about how generics work and what happens if you don't specify a type.
You got /3 concepts.
Describe how to declare a default generic type and how to override it when calling a generic function.
Remember the equals sign in the generic parameter and how to call generic functions.
You got /3 concepts.