In TypeScript, How to cast boolean to number, like 0 or 1

大憨熊 提交于 2019-12-05 01:39:08

You can't just cast it, the problem is at runtime not only at compile time.

You have a few ways of doing that:

let action: string = actions[isPlay ? 1 : 0];
let action: string = actions[+isPlay];
let action: string = actions[Number(isPlay)];

Those should be fine with both the compiler and in runtime.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!