Type narrowing is the process by which a type is refined to a more specific type.
The TypeScript compiler does its best to narrow types using the existing JavaScript code.
TIL about TypeScript type predicates, a way to be more explicit and have better control over type inference.
First, we define a function whose return type is a type predicate:
function isAudio(content: Audio | Text): content is Audio {
return (content as Audio).duration !== undefined;
}
We can now use isAudio
to narrow the type of a variable we're working with,
allowing TypeScript from that point on to know the more specific type:
let content = getContent();
if (isAudio(content)) {
content.play();
} else {
content.render();
}
Resources: