0
0
Typescriptprogramming~10 mins

Default generic types in Typescript - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Default generic types
Define generic type with default
Use generic without specifying type?
YesUse default type
No
Use generic with specified type
Function or class runs with chosen type
This flow shows how a generic type with a default is defined and used either by specifying a type or by falling back to the default.
Execution Sample
Typescript
function wrap<T = string>(value?: T): (T | undefined)[] {
  return [value];
}

const a = wrap(123);
const b = wrap('hello');
const c = wrap();
This code defines a generic function with a default type string and calls it with different arguments to show default and explicit generic usage.
Execution Table
StepCallGeneric Type UsedValue PassedReturn Value
1wrap(123)number (inferred)123[123]
2wrap('hello')string (inferred)'hello'['hello']
3wrap()string (default)undefined[undefined]
💡 All calls complete; default generic type used when no argument is passed.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3
aundefined[123][123][123]
bundefinedundefined['hello']['hello']
cundefinedundefinedundefined[undefined]
Key Moments - 3 Insights
Why does wrap() use string as the generic type even though no argument is passed?
Because the generic type T has a default of string, when no type or argument is provided, TypeScript uses string as the default type (see execution_table step 3).
How does TypeScript decide the generic type when we call wrap(123)?
TypeScript infers the generic type from the argument's type, so T becomes number for wrap(123) (see execution_table step 1).
What happens if we explicitly specify a generic type when calling wrap?
The specified type overrides the default and inference, so the function uses that type for T.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what generic type is used in step 2 when calling wrap('hello')?
Astring (default)
Bstring (inferred)
Cnumber (default)
Dany
💡 Hint
Check the 'Generic Type Used' column in execution_table row for step 2.
At which step does the function use the default generic type?
AStep 3
BStep 2
CStep 1
DNone
💡 Hint
Look for 'string (default)' in the 'Generic Type Used' column in execution_table.
If we call wrap<boolean>(true), how would the generic type and return value appear?
AGeneric type: string, Return: ['true']
BGeneric type: any, Return: [true]
CGeneric type: boolean, Return: [true]
DGeneric type: boolean, Return: true
💡 Hint
Explicit generic types override defaults and inference; return is an array of the passed value.
Concept Snapshot
Default generic types let you set a fallback type for generics.
Syntax: function fn<T = DefaultType>(arg: T) { ... }
If no type is given or inferred, default is used.
Explicit types override default and inference.
Useful for flexible, safe code with sensible defaults.
Full Transcript
This lesson shows how default generic types work in TypeScript. We define a generic function with a default type string. When calling wrap with a number or string argument, TypeScript infers the generic type from the argument. When calling wrap with no argument, the default generic type string is used. Variables a, b, and c track the returned arrays. Key points include how inference and defaults interact, and how explicit generic types override both. The quizzes check understanding of these steps.