如何将自定义类型转换为原始类型?
如何将自定义类型转换为原始类型?
我定义了一个类型
type Rating = 0 | 1 | 2 | 3 | 4 | 5 | number
现在我想做这样的事情。
let myRating:Rating = 4 let rate:number = myRating as number
如何将myRating
转换为number
原始类型?
它给我报错:
将类型“Rating”转换为类型“number”可能是一个错误,因为两种类型没有足够的重叠。如果这是有意的,请先将表达式转换为“unknown”。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