Skip to content

Instantly share code, notes, and snippets.

@hw0k
Created January 18, 2021 05:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hw0k/5817d1d4746d0bc596632277e1c4ac6a to your computer and use it in GitHub Desktop.
Save hw0k/5817d1d4746d0bc596632277e1c4ac6a to your computer and use it in GitHub Desktop.
TN 7
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