Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Pattern Matching with Discriminated Unions | Discriminated Unions and Exhaustive Handling
Quizzes & Challenges
Quizzes
Challenges
/
Error Handling and Type Guards in TypeScript

bookPattern Matching with Discriminated Unions

123456789101112131415161718
type Shape = | { kind: "circle"; radius: number } | { kind: "square"; side: number } | { kind: "rectangle"; width: number; height: number }; function area(shape: Shape): number { switch (shape.kind) { case "circle": // Only a circle has a radius property return Math.PI * shape.radius * shape.radius; case "square": // Only a square has a side property return shape.side * shape.side; case "rectangle": // Only a rectangle has width and height properties return shape.width * shape.height; } }
copy

When working with discriminated unions, you often need to write logic that depends on the specific variant of the union you are handling. By using the discriminant property—in this example, "kind"—you can pattern match on each possible case. A switch statement is a natural fit for this purpose: it allows you to branch your logic based on the value of the discriminant, and, crucially, gives you safe access to properties that exist only on a particular variant.

question mark

How does using a switch statement on the discriminant property in a discriminated union help you safely access properties in TypeScript?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3

Demandez à l'IA

expand

Demandez à l'IA

ChatGPT

Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion

Suggested prompts:

Can you explain what a discriminated union is in TypeScript?

How does the switch statement ensure type safety in this example?

Can you show how to add another shape to this union?

Awesome!

Completion rate improved to 5.88

bookPattern Matching with Discriminated Unions

Glissez pour afficher le menu

123456789101112131415161718
type Shape = | { kind: "circle"; radius: number } | { kind: "square"; side: number } | { kind: "rectangle"; width: number; height: number }; function area(shape: Shape): number { switch (shape.kind) { case "circle": // Only a circle has a radius property return Math.PI * shape.radius * shape.radius; case "square": // Only a square has a side property return shape.side * shape.side; case "rectangle": // Only a rectangle has width and height properties return shape.width * shape.height; } }
copy

When working with discriminated unions, you often need to write logic that depends on the specific variant of the union you are handling. By using the discriminant property—in this example, "kind"—you can pattern match on each possible case. A switch statement is a natural fit for this purpose: it allows you to branch your logic based on the value of the discriminant, and, crucially, gives you safe access to properties that exist only on a particular variant.

question mark

How does using a switch statement on the discriminant property in a discriminated union help you safely access properties in TypeScript?

Select the correct answer

Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 2. Chapitre 3
some-alt