Skip to content

Instantly share code, notes, and snippets.

@etienne-dldc
Created September 21, 2021 13:20
Show Gist options
  • Save etienne-dldc/387333bbe1b3294302458ca42fca252a to your computer and use it in GitHub Desktop.
Save etienne-dldc/387333bbe1b3294302458ca42fca252a to your computer and use it in GitHub Desktop.
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