Sub topic 13
Reading Errors That Involve Generics
Assume you know JavaScript functions, objects, arrays, TypeScript annotations, interfaces, and union types. You should also be comfortable running tsc and reading ordinary type mismatch errors.
Generics let a function, type, or class work with several types while preserving relationships between those types. A generic type parameter is a placeholder such as T. The compiler replaces it with a specific type when the generic is used.
Read the relationship first
Consider this function:
function first<T>(items: T[]): T {
return items[0];
}
const name = first(["Ada", "Alan"]);
const count = first([1, 2, 3]);TypeScript infers T as string for the first call and number for the second. The return type is connected to the array element type.
A generic error often means that the compiler cannot preserve the relationship promised by the type parameters.
function pair<T>(value: T, values: T[]): void {
console.log(value, values);
}
pair("ready", [1, 2, 3]);The compiler reports a TS2345-style error because T must be one type for both arguments. The first argument suggests T is string, but the array suggests T is number. Read the message as a conflict between inferred candidates, rather than as an isolated complaint about the second argument.
You can make the intended type explicit:
function pair<T>(value: T, values: T[]): void {
console.log(value, values);
}
pair<number>(1, [2, 3]);This does not make incompatible values valid. It tells the compiler which relationship to check.
Follow constraints to the root cause
A generic can have a constraint. A constraint says which types are allowed.
function getLength<T extends { length: number }>(value: T): number {
return value.length;
}
getLength("TypeScript");
getLength([1, 2, 3]);
getLength(42);The last call fails because number does not have a length property. In a longer diagnostic, read from the outer error to the nested cause:
- The call argument is not assignable to the parameter.
- The parameter uses
T extends { length: number }. - The supplied type,
number, does not satisfy that constraint. - The required
lengthproperty is missing.
Common generic mistakes
| Diagnostic pattern | Likely cause | Useful first check |
|---|---|---|
| `T is not assignable to U` | Two type parameters have no proven relationship | Add a constraint or use one parameter |
| Property does not exist on `T` | `T` is unconstrained | Add `extends` with the required property |
| Argument is not assignable to parameter | Inference selected incompatible types | Inspect each argument and specify `T` if needed |
| Type is missing properties from another type | A generic result is too broad | Narrow the type or improve the generic signature |
Choosing a fix
First decide whether the implementation, the call site, or the generic contract is wrong.
- Fix the value when it truly has the wrong type.
- Add a constraint when the implementation needs a known property.
- Use separate type parameters when two values may legitimately have different types.
- Specify a type argument when inference lacks enough information.
- Change the generic signature when it promises more than the function can guarantee.
Exercise
Why does getLength(42) fail when T extends { length: number }, and what is the safest fix?
Show answer
Number does not provide the required length property. Pass a string or array, or change the function contract if numbers should be supported.