

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Intersections of Types
incomplete
2: The Never Type
incomplete
3: Intersecting Incompatible Types
incomplete
4: Intersections vs. Unions
incomplete
5: Super Set Unions
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
In TypeScript, the never type represents values that can't occur... sounds useless, right?
Well, it's not. Take a look at this function that should handle 3 cases:
function handleStatusCode(code: 200 | 404 | 500) {
if (code === 200) {
console.log("OK");
return;
}
if (code === 404) {
console.log("Not Found");
return;
}
throw new Error(`Unknown status code: ${code}`);
}
But it only handles 200 and 404! TypeScript isn't throwing any compiler errors, but we can configure it to do so! See, after each conditional, the type of code is narrowed down:
function handleStatusCode(code: 200 | 404 | 500) {
if (code === 200) {
console.log("OK");
return;
}
// code is now 404 | 500
if (code === 404) {
console.log("Not Found");
return;
}
// code is now 500
throw new Error(`Unknown status code: ${code}`);
}
If we assign code to never, TypeScript will complain unless code has actually been narrowed down to no possible values.
function handleStatusCode(code: 200 | 404 | 500) {
if (code === 200) {
console.log("OK");
return;
}
if (code === 404) {
console.log("Not Found");
return;
}
// Type '500' is not assignable to type 'never'.
const err: never = code;
return err;
}
And now it's fixed by simply handling every case properly:
function handleStatusCode(code: 200 | 404 | 500) {
if (code === 200) {
console.log("OK");
return;
}
if (code === 404) {
console.log("Not Found");
return;
}
if (code === 500) {
console.log("Internal Server Error");
return;
}
// no errors! code is never
const err: never = code;
return err;
}
Support.ai's chatbot supports slash commands. When a developer adds a new slash command (the slashCommands union), we want TypeScript to ensure that the handleSlashCommand accounts for it.