TypeScript将文字字符串分配给标有枚举的变量

时间:2019-11-08 20:38:23

标签: typescript

给出一个带有字符串值的枚举,为什么我不能用该类型的文字字符串分配该类型的变量。

enum Testing {
    testing = 'test',
}

const foo: Testing = 'test'; // error 'test' is not assignable to type Testing

但是:

enum NumberType {
    first = 0,
}

const first: NumberType = 0; // works

0 个答案:

没有答案