【发布时间】:2020-10-26 06:19:16
【问题描述】:
我正在使用一个用于 TS 生成的库 (ts-protoc-gen),它为枚举生成以下内容:
export interface AnimalTypeMap {
Dog: 0;
Cat: 1;
Fish: 2;
Bird: 3;
}
export const AnimalType: AnimalTypeMap;
- 应该如何食用?
if (arg === AnimalType.Bird) {} // **ERROR**: Variable 'AnimalType' is used before being assigned
if (arg === AnimalTypeMap.Bird) {} // **ERROR**:'AnimalTypeMap' only refers to a type, but is being used as a value here
- 如何在不初始化的情况下导出 const?
【问题讨论】:
-
为什么不使用普通枚举?接口在运行时不存在,所以你不能做你在这里尝试的事情。
-
其他选项会将其导出为值:
export const AnimalType = { Dog: 0, Cat: 1 } as const
标签: typescript enums interface protobuf.js