作者:amwaysuju | 来源:互联网 | 2022-11-24 18:38
我有一个类型
type Rating = 0 | 1 | 2 | 3 | 4 | 5 | number
现在我想做这样的事情。
let myRating:Rating = 4
let rate:number = myRating as number
如何将我myRating
转换为number
原始类型?
它给我的错误是:
从“评级”类型到“数字”类型的转换可能是错误的,因为这两种类型都没有足够的重叠。如果这是故意的,请先将表达式转换为“未知”。ts(2352)
我已经经历过了,但是我想要的是它的逆转
编辑:
tsconfig.json
{
"compilerOptions": {
"noImplicitAny": false,
"target": "es6",
"allowJs": true,
"skipLibCheck": false,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "preserve",
"strict": true
},
"include": [
"src"
]
}
tsc版本:3.2.1
1> Steven Spung..:
尝试:
myRating as unknown as number
另外,| number
从声明中删除。