Skip to content

Instantly share code, notes, and snippets.

@simnalamburt
Last active August 3, 2022 09:50
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simnalamburt/c3af8d291ff66df08512d7d96956b32c to your computer and use it in GitHub Desktop.
Save simnalamburt/c3af8d291ff66df08512d7d96956b32c to your computer and use it in GitHub Desktop.
TypeScript vs Flow
// TypeScript 2.2, compiled with: --strictNullChecks
'use strict'
type PaymentMethod =
{ kind: 'cash', receipt() } |
{ kind: 'paypal', email() } |
{ kind: 'credit', activeX() }
function describePaymentMethod(method: PaymentMethod): string {
switch (method.kind) {
//case 'ㅇㅅㅇ': return 'Nooo' // NOTE: 이 코드 주석을 해제하면, 타입 에러가 남
case 'cash':
method.receipt()
return '캐시'
case 'paypal':
method.email()
return '페이팔'
case 'credit':
method.activeX()
//method.email() // NOTE: 이 코드 주석을 해제하면, 타입 에러가 남
return '카드' // NOTE: 이 코드 주석처리하면 타입에러가 남
}
}
// @flow
// v0.44.1
'use strict'
type PaymentMethod =
{| kind: 'cash', receipt: () => void |} |
{| kind: 'paypal', email: () => void |} |
{| kind: 'credit', activeX: () => void |};
// NOTE: exhaustive하게 처리해줬음에도 불구하고, 컴파일러가
// 인지를 못해서 불필요한 default clause를 넣어야 함!!!!
function describePaymentMethod(method: PaymentMethod): string {
switch (method.kind) {
//case 'ㅇㅅㅇ': return 'Nooo'; // NOTE: 이 코드 주석을 해제해도, 타입 에러가 나지 않음!!!!
default: return 'ㅇㅅㅇ;;'; // NOTE: 이 코드가 없어도 컴파일 되어야 정상임!!!
case 'cash':
method.receipt();
return '현금';
case 'paypal':
method.email()
return '페이팔'
case 'credit':
method.activeX()
//method.email() // NOTE: 이 코드 주석을 해제하면, 타입 에러가 남
return '카드' // NOTE: 이 코드 주석처리하면 타입에러가 남
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment