Explicit Type Arguments
What you'll learn: How to manually override TypeScript's automatic type inference by specifying generic type arguments yourself.
The Problem with Inference
While TypeScript usually infers generic type arguments automatically (as you learned in "Type Parameter Inference"), sometimes the inference either fails, chooses the wrong type, or you want to override it for clarity or correctness.
Explicit type arguments let you manually specify what types should be used for type parameters, giving you full control when you need it.
The Syntax
You've seen TypeScript infer types like this:
function identity<T>(value: T): T {
return value;
}
const result = identity("hello"); // T inferred as "hello"
But you can explicitly specify the type argument using angle brackets:
const result = identity<string>("hello"); // T is string, not "hello"
The <string> before the function call tells TypeScript exactly which type to use for T.
When You Need This
- When inference fails (TypeScript can't figure it out)
- When inference is too narrow (literal instead of primitive)
- When inference is too wide (you want more specific constraints)
- For code clarity (making types obvious to readers)
function makeArray<T>(value: T): T[] {
return [value];
}
const nums = makeArray<number>(5); // Explicit: number[]
const strs = makeArray("hi"); // Inferred: string[]
Key Takeaway: Explicit type arguments let you take manual control of generics when inference doesn't do what you need—think of them as your override switch for TypeScript's automatic type selection.