Skip to content

Instantly share code, notes, and snippets.

@seungha-kim
Created September 22, 2019 12:21
Show Gist options
  • Save seungha-kim/c52dc56f6c39487b52a2da0eab2aabe2 to your computer and use it in GitHub Desktop.
Save seungha-kim/c52dc56f6c39487b52a2da0eab2aabe2 to your computer and use it in GitHub Desktop.
const code: string = 'en' // string?
type LanguageCode = 'ko' | 'en' | 'ja' | 'zh' | 'es'
const code: LanguageCode = 'ko'
const korean = 'ko'
const english = 'en'
const japanese = 'ja'
const chinese = 'zh'
const spanish = 'es'
// 이렇게 하면 언어 코드가 위아래에 중복되고
type LanguageCode = 'ko' | 'en' | 'ja' | 'zh' | 'es'
// 이렇게 하면 코드가 너무 길어집니다
// type LanguageCode = typeof korean | typeof english | typeof japanese | typeof chinese | typeof spanish
const code: LanguageCode = korean
export enum LanguageCode {
korean = 'ko',
english = 'en',
japanese = 'ja',
chinese = 'zh',
spanish = 'es',
}
// 여기서
// LanguageCode.korean === 'ko'
// (의미상) LanguageCode === 'ko' | 'en' | 'ja' | 'zh' | 'es'
const code: LanguageCode = LanguageCode.korean
const keys = Object.keys(LanguageCode) // ['korean', 'english', ...]
const values = Object.values(LanguageCode) // ['ko', 'en', ...]
const LanguageCode = {
korean: 'ko',
english: 'en',
japanese: 'ja',
chinese: 'zh',
spanish: 'es',
} as const
// 속성 값을 변경할 수 없음
// 속성의 타입으로 리터럴 타입이 지정됨
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment