Created
March 13, 2020 01:20
-
-
Save ksaldana1/54819f40d718d9ba5a1a81d85022185c to your computer and use it in GitHub Desktop.
gist_2
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
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