Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Oppiskele The never Type and assertNever Pattern | Error Handling and Advanced Narrowing
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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1

Kysy tekoälyä

expand

Kysy tekoälyä

ChatGPT

Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme

Awesome!

Completion rate improved to 5.88

bookThe never Type and assertNever Pattern

Pyyhkäise näyttääksesi valikon

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

Oliko kaikki selvää?

Miten voimme parantaa sitä?

Kiitos palautteestasi!

Osio 3. Luku 1
some-alt