Skip to content

Instantly share code, notes, and snippets.

@ksaldana1
Created March 13, 2020 01:20
Show Gist options
  • Save ksaldana1/54819f40d718d9ba5a1a81d85022185c to your computer and use it in GitHub Desktop.
Save ksaldana1/54819f40d718d9ba5a1a81d85022185c to your computer and use it in GitHub Desktop.
gist_2
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
interface Triangle {
kind: "triangle";
base: number;
height: number
}
type Shape = Square | Rectangle | Circle | Triangle;
// This is now giving us an error
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;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment