Sub topic 14
Comparing Generic Types
Assume you know TypeScript interfaces, type aliases, functions, and compiler errors. This chapter focuses on how TypeScript compares types that contain type parameters.
A generic type is a type definition that receives another type as an argument. Box<string> and Box<number> are two different instantiations of the same generic definition.
Type arguments affect compatibility
TypeScript compares the structure of the instantiated types. The name of the generic type alone does not decide compatibility.
interface Producer<T> {
get(): T;
}
const strings: Producer<string> = {
get: () => "hello"
};
const values: Producer<unknown> = strings; // OK
const text: Producer<string> = values; // ErrorA Producer<string> can produce a value that is also an unknown. The reverse is unsafe because a producer of unknown might return a number.
A type parameter that does not appear in the structure may have no effect on compatibility:
interface Marker<T> {
label: string;
}
const textMarker: Marker<string> = { label: "text" };
const numberMarker: Marker<number> = textMarker; // OKBoth instantiations have the same effective shape. TypeScript therefore ignores the unused T when comparing them.
| Generic relationship | Typical result | Reason |
|---|---|---|
| `Producer<string>` to `Producer<unknown>` | Allowed | The produced value is safely widened |
| `Producer<unknown>` to `Producer<string>` | Rejected | An unknown value may not be a string |
| `Marker<string>` to `Marker<number>` | Allowed | `T` does not affect any member |
Input and output positions
A type parameter is in an output position when it appears in a returned value. It is in an input position when it appears in a function parameter.
interface Consumer<T> {
set(value: T): void;
}
declare let stringConsumer: Consumer<string>;
declare let unknownConsumer: Consumer<unknown>;
stringConsumer = unknownConsumer; // OK
unknownConsumer = stringConsumer; // ErrorA consumer of unknown can accept every string, so it can replace a consumer that only needs strings. A consumer of strings cannot safely replace one that might receive any value.
When a type parameter appears in both input and output positions, the type usually needs invariant behavior. Invariance means that neither Container<A> nor Container<B> can replace the other merely because A and B are related.
interface MutableBox<T> {
get(): T;
set: (value: T) => void;
}MutableBox<string> and MutableBox<unknown> should not be interchangeable. Reading requires one direction of safety, while writing requires the opposite direction.
Generic functions
Type parameter names do not matter when comparing equivalent generic call signatures.
type IdentityA = <T>(value: T) => T;
type IdentityB = <U>(value: U) => U;
const identity: IdentityA = (value) => value;
const sameIdentity: IdentityB = identity; // OKThe functions have the same generic behavior. Constraints and member usage still matter. A function that accepts only T extends string cannot automatically replace one that accepts every T.
Exercise
Why is Marker<string> assignable to Marker<number> when Marker<T> has no member that uses T?
Show answer
The two instantiated types have the same effective structure. Since T does not affect any member, the type argument does not affect assignability.