Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära The never Type and assertNever Pattern | Error Handling and Advanced Narrowing
Quizzes & Challenges
Quizzes
Challenges
/
Error Handling and Type Guards in TypeScript

bookThe never Type and assertNever Pattern

When working with discriminated unions in TypeScript, it is essential to ensure that every possible variant is handled in your logic. The never type and the assertNever pattern are powerful tools for enforcing exhaustive checks, especially in switch statements. By leveraging these, you can catch missing cases at compile time, preventing subtle bugs that might otherwise appear when new variants are added to a union type. This practice is especially important for code maintainability and safety as your codebase grows.

123456789101112131415161718
type Shape = | { kind: "circle"; radius: number } | { kind: "square"; side: number }; function assertNever(x: never): never { throw new Error("Unexpected object: " + x); } function area(shape: Shape): number { switch (shape.kind) { case "circle": return Math.PI * shape.radius * shape.radius; case "square": return shape.side * shape.side; default: return assertNever(shape); // Compile-time error if a new kind is added and not handled } }
copy

In this example, the assertNever function takes a parameter of type never. If the switch statement is missing a case for a new variant of the Shape union, TypeScript will flag a compile-time error at the call to assertNever. This ensures that all possible variants are considered, making it much less likely to overlook a case when the union is extended. Exhaustive checking using never and assertNever is critical for maintainability because it forces you to update all relevant logic whenever your union types evolve, reducing the risk of runtime errors caused by unhandled cases.

question mark

Which of the following best describes the purpose of the never type and assertNever pattern in switch statements over discriminated unions?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Awesome!

Completion rate improved to 5.88

bookThe never Type and assertNever Pattern

Svep för att visa menyn

When working with discriminated unions in TypeScript, it is essential to ensure that every possible variant is handled in your logic. The never type and the assertNever pattern are powerful tools for enforcing exhaustive checks, especially in switch statements. By leveraging these, you can catch missing cases at compile time, preventing subtle bugs that might otherwise appear when new variants are added to a union type. This practice is especially important for code maintainability and safety as your codebase grows.

123456789101112131415161718
type Shape = | { kind: "circle"; radius: number } | { kind: "square"; side: number }; function assertNever(x: never): never { throw new Error("Unexpected object: " + x); } function area(shape: Shape): number { switch (shape.kind) { case "circle": return Math.PI * shape.radius * shape.radius; case "square": return shape.side * shape.side; default: return assertNever(shape); // Compile-time error if a new kind is added and not handled } }
copy

In this example, the assertNever function takes a parameter of type never. If the switch statement is missing a case for a new variant of the Shape union, TypeScript will flag a compile-time error at the call to assertNever. This ensures that all possible variants are considered, making it much less likely to overlook a case when the union is extended. Exhaustive checking using never and assertNever is critical for maintainability because it forces you to update all relevant logic whenever your union types evolve, reducing the risk of runtime errors caused by unhandled cases.

question mark

Which of the following best describes the purpose of the never type and assertNever pattern in switch statements over discriminated unions?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 3. Kapitel 1
some-alt