Sub topic 17
Enum Compatibility and Intentional Unsoundness
This chapter assumes basic TypeScript syntax, interfaces, functions, arrays, and compiler errors. It also assumes strict mode is enabled unless stated otherwise.
Enum compatibility
An enum creates a named set of related values. TypeScript checks enum types more strictly than ordinary object shapes. Two separately declared enums are incompatible even when they have members with the same names and values.
enum Status {
Ready,
Done,
}
enum OtherStatus {
Ready,
Done,
}
const status: Status = Status.Ready;
const numberValue: number = Status.Ready; // OK
// @ts-expect-error Separate enum declarations are incompatible.
const other: Status = OtherStatus.Ready;Numeric enum members can be assigned to number because each member has a numeric value. The reverse direction is less safe. A general number does not communicate which enum member it represents, although TypeScript has historically allowed numbers in some numeric-enum positions for compatibility.
String enums behave more like nominal types. Their values are strings at runtime, but an arbitrary string is not assignable to the enum.
enum Direction {
Up = "UP",
Down = "DOWN",
}
const direction: Direction = Direction.Up;
// @ts-expect-error A plain string is not a Direction.
const invalid: Direction = "UP";
const text: string = Direction.Up; // OKEnum compatibility is therefore directional in two ways. An enum member can often be used where its underlying primitive is expected, but the primitive does not necessarily satisfy the enum type. Separate enum declarations do not merge based on their structure.
Where assignability is intentionally unsound
TypeScript favors useful JavaScript patterns over proving that every program is mathematically safe. This creates known unsound cases: code can pass the type checker and still fail at runtime.
Array covariance is one example. If Dog extends Animal, TypeScript allows Dog[] to be assigned to Animal[]. That makes reading safe, but it also permits an Animal that is not a Dog to be written through the wider reference.
interface Animal {
name: string;
}
interface Dog extends Animal {
bark(): void;
}
const dogs: Dog[] = [];
const animals: Animal[] = dogs;
animals.push({ name: "cat" }); // Allowed
// dogs[0].bark(); // Runtime failure: this value is not a DogReadonly arrays avoid this write hole. A readonly Dog[] can be viewed as a readonly Animal[] because callers cannot insert an incompatible value.
Function parameters have another intentional exception. With strictFunctionTypes, function-valued properties are checked contravariantly, but methods remain bivariant for compatibility with existing class and interface patterns. A method accepting only Dog can therefore be accepted where a method accepting any Animal is expected, even though a caller may provide a non-Dog.
Prefer readonly collections and function-valued properties when you want stricter checking. These choices make the intended variance visible to the compiler.
Exercise
Why is readonly Dog[] safer to assign to readonly Animal[] than Dog[] is to Animal[]?
Show answer
A readonly array supports reading but not writing, so the wider reference cannot insert an Animal that is not a Dog.