In TypeScript, How to cast boolean to number, like 0 or 1
You can convert anything to boolean and then to a number by using +!!
:
const action: string = actions[+!!isPlay]
This can be useful when for example you want at least two out of three conditions to hold, or exactly one to hold:
const ok = (+!!something) + (+!!somethingelse) + (+!!thirdthing) > 1
const ok = (+!!something) + (+!!somethingelse) + (+!!thirdthing) === 1
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.