TypeScript: Conditional types produce incorrect typings when used with literal types
Bug Report
🔎 Search Terms
Conditional types, union types, literal types, boolean
🕗 Version & Regression Information
- This is the behavior in every version I tried, and I reviewed the FAQ for entries about generics, union types and booleans
⏯ Playground Link
Playground link with relevant code
💻 Code
type Test<T> = T extends never ? never[] : T[];
let x: Test<boolean> = [true, false];
let y: Test<3|4|5> = [5,3];
🙁 Actual behavior
Compiles with errors
Type 'boolean[]' is not assignable to type 'false[] | true[]'.
Type 'boolean[]' is not assignable to type 'false[]'.
Type 'boolean' is not assignable to type 'false'.
Type '(3 | 5)[]' is not assignable to type '3[] | 4[] | 5[]'.
Type '(3 | 5)[]' is not assignable to type '3[]'.
Type '3 | 5' is not assignable to type '3'.
Type '5' is not assignable to type '3'.
x is of type true[] | false[]
y is of type 3[] | 4[] | 5[]
🙂 Expected behavior
x be of type boolean[]
y be of type (3|4|5)[]
Compiles with no errors
About this issue
- Original URL
- State: closed
- Created a year ago
- Comments: 15 (3 by maintainers)
I think identifying the cases where the user mistakingly used distributive types instead of just making another mistake is fairly difficult.