Created
January 18, 2021 05:27
-
-
Save hw0k/5817d1d4746d0bc596632277e1c4ac6a to your computer and use it in GitHub Desktop.
TN 7
This file contains 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 Circle { | |
kind: 'circle'; | |
radius: number; | |
} | |
interface Square { | |
kind: 'square'; | |
sideLength: number; | |
} | |
type Shape = Circle | Square; | |
function getArea(shape: Shape) { | |
switch (shape.kind) { | |
case 'circle': | |
// shape는 Circle로 추론됨. | |
return shape.radius * shape.radius * Math.PI; | |
case 'square': | |
// shape는 Square로 추론됨. | |
return shape.sideLength * shape.sideLength; | |
default: | |
throw new Error('Not implemented shape.'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment