Sub topic 3
What you can access on a union before narrowing
The shared view of a union
Prerequisites: basic TypeScript type annotations, object types, type aliases, and function types.
A union type describes a value that can have one of several types. The | operator creates a union:
type Cat = {
name: string;
meow(): void;
};
type Dog = {
name: string;
bark(): void;
};
function describePet(pet: Cat | Dog): string {
return pet.name;
}Inside describePet, pet might be a Cat or a Dog. TypeScript therefore allows access only to members that exist on both types. Both types have a name property, so pet.name is safe.
The union does not mean that pet has every member from both types. It means that the value has at least one of the listed shapes, but the compiler does not yet know which one.
Shared properties and methods
A member is a property or method defined by a type. A union member is available before narrowing only when every type in the union provides that member.
| Expression | Available on `Cat | Dog`? | Reason |
|---|---|---|---|
| `pet.name` | Yes | Both types define `name` | |
| `pet.meow()` | No | `Dog` does not define `meow` | |
| `pet.bark()` | No | `Cat` does not define `bark` |
The compiler rejects code that assumes a union has a member belonging to only one possibility:
type Cat = {
name: string;
meow(): void;
};
type Dog = {
name: string;
bark(): void;
};
function printName(pet: Cat | Dog): void {
console.log(pet.name);
// pet.meow(); // Error: Dog might not have meow
// pet.bark(); // Error: Cat might not have bark
}The commented lines would be unsafe. If the function received a Dog, calling meow would fail at runtime. TypeScript prevents this mistake before the program runs.
A shared member can also have a union type. Consider two object types that use the same property name with different value types:
type TextMessage = {
content: string;
format: "text";
};
type ImageMessage = {
content: Uint8Array;
format: "image";
};
function inspectMessage(message: TextMessage | ImageMessage): void {
console.log(message.content);
console.log(message.format);
}Both content and format are available. The type of message.content is string | Uint8Array, because the actual property type depends on which kind of message was supplied. The type of message.format is "text" | "image".
Why narrowing is needed
TypeScript uses the safe, common view until you provide evidence about the actual type. Narrowing is the process of checking a value so that TypeScript can reduce a union to one of its constituent types.
For example, a property check can identify the specific pet:
type Cat = {
name: string;
meow(): void;
};
type Dog = {
name: string;
bark(): void;
};
function describePet(pet: Cat | Dog): string {
if ("meow" in pet) {
pet.meow();
return `${pet.name} is a cat`;
}
pet.bark();
return `${pet.name} is a dog`;
}Before the if, only name is guaranteed. Inside the first branch, "meow" in pet tells TypeScript that pet is a Cat. After that branch, the remaining possibility is Dog, so pet.bark() is safe.
Exercise
Given type A = { id: string; open(): void } and type B = { id: string; close(): void }, which members can a value of type A | B access before narrowing?
Show answer
It can access id, because both types define it. It cannot access open or close, because each method exists on only one type.