nishimura.clubnishimura.club

TypeScriptでEnumを使用せずそれっぽいものを作る

作成日
2022-03-08
更新日
2022-04-25
export const PeopleType = { A: 'A', B: 'B', C: 'C', } as const export type PeopleType = typeof PeopleType[keyof typeof PeopleType]

TypeScriptのenumを使わないほうがいい理由を、Tree-shakingの観点で紹介します - LINE ENGINEERING

Memo

as constを使用しないと、PeopleTypestringとして推論される

const str: PeopleType = 'Text' // error発生しない

Related

Tags

TypeScript

Share

Table of Contents