Created
September 21, 2021 13:20
-
-
Save etienne-dldc/387333bbe1b3294302458ca42fca252a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type Rectangle = { | |
kind: "rectangle"; | |
width: number; | |
height: number; | |
}; | |
type Circle = { | |
kind: "circle"; | |
radius: number; | |
}; | |
type Triangle = { | |
kind: "triangle"; | |
base: number; | |
}; | |
type Oval = { | |
kind: "oval"; | |
width: number; | |
height: number; | |
}; | |
function drawShape(shape: Circle | Rectangle | Triangle | Oval): number { | |
if (shape.kind === "circle") { | |
return shape.radius; | |
} | |
if (shape.kind === "rectangle") { | |
return shape.height * shape.width; | |
} | |
if (shape.kind === "triangle") { | |
return shape.base; | |
} | |
if (shape.kind === 'oval') { | |
return shape.height * shape.width; | |
} | |
expectNever(shape); | |
} | |
function expectNever(_nev: never): never { | |
throw new Error("Unexpected never"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment