有没有办法让 TypeScript 枚举与 JSON 中的字符串兼容?
例如:
enum Type { NEW, OLD }
interface Thing { type: Type }
let thing:Thing = JSON.parse('{"type": "NEW"}');
alert(thing.type == Type.NEW); // false
I would like thing.type == Type.NEW
是真实的。或者更具体地说,我希望我可以指定enum
值定义为strings,不是数字。
我知道我可以使用thing.type.toString() == Type[Type.NEW]
但这很麻烦,并且似乎使枚举类型注释变得混乱和误导,这违背了它的目的。 JSON 从技术上讲是not提供有效的枚举值,因此我不应该将属性键入到枚举中。
所以我目前正在做的是使用带有静态常量的字符串类型:
const Type = { NEW: "NEW", OLD: "OLD" }
interface Thing { type: string }
let thing:Thing = JSON.parse('{"type": "NEW"}');
alert(thing.type == Type.NEW); // true
这让我得到了我想要的用法,但是类型注释string
太宽泛而且容易出错。
我有点惊讶 JavaScript 的超集没有基于字符串的枚举。我错过了什么吗?有其他方法可以做到这一点吗?
更新TS 1.8
Using 字符串文字类型 https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#string-literal-types是另一种选择(感谢@basaret),但是为了获得所需的类似枚举的用法(上面),它需要定义您的值twice:一次是字符串文字类型,一次是作为值(常量或命名空间):
type Type = "NEW" | "OLD";
const Type = {
NEW: "NEW" as Type,
OLD: "OLD" as Type
}
interface Thing { type: Type }
let thing:Thing = JSON.parse(`{"type": "NEW"}`);
alert(thing.type === Type.NEW); // true
这可行,但需要大量样板,足以让我大部分时间不使用它。现在我希望提案string enums https://github.com/Microsoft/TypeScript/issues/1206#issuecomment-240581743最终将制定路线图。
更新TS 2.1
The new keyof类型查找 https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#keyof-and-lookup-types允许从 const 或命名空间的键生成字符串文字类型,这使得定义成为little减少冗余:
namespace Type {
export const OLD = "OLD";
export const NEW = "NEW";
}
type Type = keyof typeof Type;
interface Thing { type: Type }
const thing: Thing = JSON.parse('{"type": "NEW"}');
thing.type == Type.NEW // true
更新TS 2.4
TypeScript 2.4 添加了对字符串枚举的支持 https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#string-enums!上面的例子就变成:
enum Type {
OLD = "OLD",
NEW = "NEW"
}
interface Thing { type: Type }
const thing: Thing = JSON.parse('{"type": "NEW"}');
alert(thing.type == Type.NEW) // true
这看起来nearly完美,但还是有一些心痛:
- You still必须将该值写入两次,即
OLD = "OLD"
,并且没有验证您没有输入错误,例如NEW = "MEW"
...这已经在实际代码中困扰了我。
-
枚举的类型检查方式存在一些奇怪之处(也许是错误?),它不仅仅是字符串文字类型简写,这才是真正正确的。我遇到的一些问题:
enum Color { RED = "RED", BLUE = "BLUE", GREEN = "GREEN" }
type ColorMap = { [P in Color]: number; }
declare const color: Color;
declare const map: ColorMap;
map[color] // Error: Element implicitly has an 'any' type because type 'ColorMap' has no index signature.
const red: Color = "RED"; // Type '"RED"' is not assignable to type 'Color'.
const blue: Color = "BLUE" as "RED" | "BLUE" | "GREEN"; // Error: Type '"RED" | "BLUE" | "GREEN"' is not assignable to type 'Color'.
等效代码为enum Color
替换为字符串文字类型工作正常......
是的,我想我对此有强迫症,我只想要完美的 JS 枚举。 :)