Skip to content

Instantly share code, notes, and snippets.

@ksaldana1
Created March 13, 2020 01:21
Show Gist options
  • Save ksaldana1/00622fb13a9bdbbd170d124fbebde568 to your computer and use it in GitHub Desktop.
Save ksaldana1/00622fb13a9bdbbd170d124fbebde568 to your computer and use it in GitHub Desktop.
gist_3
function assertNever(x: never): never {
throw new Error("Unexpected object: " + x);
}
function area(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.height * s.width;
case "circle": return Math.PI * s.radius ** 2;
default: return assertNever(s); // error here if there are missing cases
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment