When working in TypeScript, I came across an interesting observation when compiling the following code:
const x = true as false;
Surprisingly, this direct assertion is valid, creating a constant x
with the value true
and type false
. This differs from the expected behavior of such assertions being invalid. For example, attempting similar code:
const x = 0 as false;
results in a compilation error.
The conversion of type 'number' to type 'false' raises a warning due to insufficient overlap between the types. To proceed intentionally, it's recommended to convert the expression to 'unknown' first.
Given this outcome, I was left wondering why no error occurs with true
and false
- and whether there might be specific compiler options or settings that could address this inconsistency.