【发布时间】:2022-10-07 22:18:09
【问题描述】:
export const files = ['a', 'b', 'c']
export type File = 'a' | 'b' | 'c'
如何在不重复 'a', 'b', 'c' 之类的值的情况下声明此文件类型。
【问题讨论】:
标签: typescript
export const files = ['a', 'b', 'c']
export type File = 'a' | 'b' | 'c'
如何在不重复 'a', 'b', 'c' 之类的值的情况下声明此文件类型。
【问题讨论】:
标签: typescript
您可以使用typeof 和number 作为索引语法。请注意,数组定义中需要 as const:
export const files = ['a', 'b', 'c'] as const;
export type File = typeof files[number];
【讨论】: