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:
function example<T = number>(value: T): T { return value; }
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() {}
AT is a function returning string
BT must always be string
CT is a variable of type string
DT is a generic type defaulting to string if not specified
✗ 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?
AIt uses the default generic type
BIt causes a compile error
CIt uses any type
DIt uses void 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?
ABy passing the type as a function argument
BBy specifying the type in angle brackets after the function name
CBy changing the default type in the function definition
DYou cannot override the default type
✗ Incorrect
You override the default by specifying the type explicitly, like foo().
Which of these is a benefit of default generic types?
AThey reduce the need to specify types every time
BThey make code run faster
CThey prevent any type errors
DThey force all generics to be strings
✗ 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?
ATypeScript throws an error
BThe default type is used anyway
CThe specified type replaces the default
DThe function ignores the generic type
✗ 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.
Practice
(1/5)
1.
What is the main purpose of default generic types in TypeScript?
easy
A. To provide a fallback type when no type argument is specified
B. To enforce a specific type always
C. To disable generics in a function
D. To convert types to strings automatically
Solution
Step 1: Understand generic types
Generic types allow functions or classes to work with any type specified by the user.
Step 2: Role of default generic types
Default generic types provide a fallback type if the user does not specify one explicitly.
Final Answer:
To provide a fallback type when no type argument is specified -> Option A
Quick Check:
Default generic types = fallback type [OK]
Hint: Defaults fill in missing generic types automatically [OK]
Common Mistakes:
Thinking default generics enforce a fixed type
Confusing default generics with type conversion
Believing default generics disable generics
2.
Which of the following is the correct syntax to set a default generic type T to string in a function?
function example<T = string>()
easy
A. function example<T extends string>()
B. function example<T: string>()
C. function example<T = string>()
D. function example<T? = string>()
Solution
Step 1: Recall TypeScript generic syntax
Default generic types use the equals sign (=) to assign a fallback type.
Step 2: Analyze options
function example<T = string>() uses <T = string> which is correct syntax for default generic types.
Final Answer:
function example<T = string>() -> Option C
Quick Check:
Default generic syntax uses = [OK]
Hint: Use = to assign default generic types [OK]
Common Mistakes:
Using colon (:) instead of equals (=)
Adding question mark (?) incorrectly
Confusing extends with default assignment
3.
What will be the output of the following TypeScript code?
function wrap<T = number>(value: T): T[] {
return [value];
}
const result = wrap('hello');
console.log(result);
medium
A. ["number"]
B. ["hello"]
C. ["value"]
D. Type error
Solution
Step 1: Understand default generic type usage
The function wrap has a default generic type T = number, but the caller passes a string 'hello', so T becomes string.
Step 2: Analyze function output
The function returns an array containing the passed value, so it returns ["hello"].
Final Answer:
["hello"] -> Option B
Quick Check:
Explicit type overrides default generic [OK]
Hint: Passed type overrides default generic type [OK]
Common Mistakes:
Assuming default generic always applies
Expecting a type error for different type
Confusing value with type name
4.
Identify the error in this TypeScript code using default generic types:
function identity<T = number>(arg: T): T {
return arg.length;
}
medium
A. Property 'length' may not exist on type 'T'
B. No error, code is correct
C. Return type should be number, not T
D. Default generic type cannot be 'number'
Solution
Step 1: Analyze the generic type and usage
The generic type T defaults to number, but number does not have a length property.
Step 2: Identify the error
Accessing arg.length causes a TypeScript error because length may not exist on type T.
Final Answer:
Property 'length' may not exist on type 'T' -> Option A
Quick Check:
Accessing unknown properties on generic types causes errors [OK]
Hint: Check if generic type supports accessed properties [OK]
Common Mistakes:
Ignoring property existence on generic types
Thinking default generic restricts properties
Assuming no error if default is number
5.
Given this generic function with a default type, how can you call it to get an array of booleans?