Closed
Description
TypeScript Version: 3.3.0-dev.20190108
Search Terms:
- type true
- type propagation
- type inference
- constant condition
- constant folding
Code
declare const __TRUE__: true;
function ensureValue<T>(value: T | null): T {
if (__TRUE__) {
if (value === null) {
throw new Error();
}
}
return value;
}
Expected behavior:
ensureValue
has a correct return type when strictNullChecks
is enabled.
In other words, if a constant is typed as true
, I expect it to behave like true
literal.
declare const __TRUE__: true
if (true) {
if (__TRUE__) {
In the above example, both if
statements should infer the types the same way.
Actual behavior:
There is a type error when strictNullChecks
is enabled:
$ tsc --strictNullChecks --noEmit test.ts
test.ts:8:3 - error TS2322: Type 'T | null' is not assignable to type 'T'.
Type 'null' is not assignable to type 'T'.
8 return value;
~~~~~~~~~~~~~
Found 1 error.
Please note that if (true)
works as expected.
Playground Link:
Related Issues: