-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Closed
Description
TypeScript Version:
3.3.4000
Search Terms:
I used the search results provided when typing this issue's title and collected the results in the Related Issues section.
Code
type T1 = { flag: true; } | { flag: false; };
type T2 = true | false;
const flag = confirm('true | false');
const demo1: T1 = { flag };
const demo2: T2 = flag;
type T3 = { flag: true; value: 'a' | 'b' | 'c'; } | { flag: false; value: 'a' | 'b'; };
const value = 'a'; // Matches `'a' | 'b'`
const demo3: T3 = { flag, value: 'a' };Expected behavior:
The code compiles with no errors.
Actual behavior:
The compiler displays an error on demo1 and demo3 lines, saying that boolean is not assignable to false. For this error to go away, one has to wrap the line in an if-else statement, passing a literal true in the if branch and false in the else branch.
The TypeScript compiler inference is probably not realizing that the rest of the union type members are compatible whichever way flag goes.
Playground Link:
Related Issues:
- Type 'boolean' is not assignable to type 'true' when constructing discriminated union variant #10657 which says see Bug: Boolean type literals misbehaving #10432 which says fixed in Fix boolean literal types #10577 which says released in
release-2.0 - Use of implicit union return type causes "'boolean' is not assignable to type 'false'" #19212 which seems to be about inheritance issues which I do not demonstrate
true|falsecan not be used to create discriminated unions #17882 where the union members are not compatible betweentrueandfalsewhich mine are
Metadata
Metadata
Assignees
Labels
No labels