Sub topic 11
Generic Types and Unused Type Parameters
Assume you know basic TypeScript types, interfaces, type aliases, and JavaScript objects. This chapter focuses on how TypeScript compares two generic types after their type arguments are supplied.
A generic type is a type definition with a placeholder. The placeholder lets the same definition work with different types:
interface Box<T> {
value: T;
}
const numberBox: Box<number> = { value: 42 };
const textBox: Box<string> = { value: "hello" };Box<number> and Box<string> are different type expressions. TypeScript does not automatically treat them as compatible. It compares the members produced by each type expression.
Type parameters matter only when they affect the shape
A type parameter affects compatibility when it appears in a member. In Box<T>, the value property uses T, so the resulting property types matter:
interface Box<T> {
value: T;
}
const numberBox: Box<number> = { value: 42 };
const textOrNumberBox: ReadonlyBox<number | string> = numberBox;
interface ReadonlyBox<T> {
readonly value: T;
}The assignment works because a number can be used wherever a number | string is expected. The property is readonly, so the wider box cannot be used to place a string into the original number box.
If the type parameter does not appear in any member, different type arguments produce the same observable shape:
interface Tag<T> {}
const stringTag: Tag<string> = {};
const numberTag: Tag<number> = stringTag;
console.log(stringTag, numberTag);Tag<string> and Tag<number> are compatible because both describe an object with no members. The type parameter exists in the declaration, but it has no effect on the structure that TypeScript checks.
A type parameter used in a method still matters
A type parameter can appear in a property, method, or function signature. Its position affects compatibility:
interface Producer<T> {
get(): T;
}
interface Consumer<T> {
set: (value: T) => void;
}
const numbers: Producer<number> = {
get: () => 10
};
const values: Producer<number | string> = numbers;
const acceptsNumbers: Consumer<number> = {
set: value => console.log(value)
};
const acceptsNumbersOrText: Consumer<number | string> = acceptsNumbers;The producer returns a number, so it can be used as a producer of numbers or strings. A consumer has the opposite concern: it must safely accept every value the target type might provide. With strictFunctionTypes enabled, TypeScript checks function parameters carefully. The last assignment is rejected because acceptsNumbers cannot safely accept a string.
Most projects enable strict checking with this configuration:
{
"compilerOptions": {
"strict": true
}
}Generic compatibility is not nominal
TypeScript does not require two generic declarations to have the same name. It compares their members:
interface Result<T> {
value: T;
}
interface Wrapped<T> {
value: T;
}
const result: Result<number> = { value: 7 };
const wrapped: Wrapped<number> = result;
console.log(wrapped.value);Result<number> is assignable to Wrapped<number> because both have a value property of type number. The interface names do not create separate nominal types.
Check unused parameters deliberately
An unused type parameter is often a design warning. It may mean that the parameter was added for a future feature, or that the type was intended to distinguish values but does not actually do so.
When the distinction matters, add a member that records it, or use a brand.
interface UserToken {
readonly kind: "user";
readonly value: string;
}
interface OrderToken {
readonly kind: "order";
readonly value: string;
}
const userToken: UserToken = { kind: "user", value: "u1" };
// @ts-expect-error: the discriminating member does not match
const orderToken: OrderToken = userToken;
console.log(orderToken);Exercise
Are interface Marker<T> {} and interface Marker<U> {} different for assignability when instantiated with different type arguments? Explain why.
Show answer
No. Neither parameter appears in a member, so both instantiations have the same empty shape. The type arguments do not affect compatibility.