-
Notifications
You must be signed in to change notification settings - Fork 13k
Closed
Labels
DuplicateAn existing issue was already createdAn existing issue was already created
Description
TypeScript Version: 3.2.0-dev.20181004
Code
const f = <T>(a: number) => {
const b: (T extends { } ? number : number) = a;
};
Expected behavior: Successful compilation
Actual behavior: I get the following error:
error TS2322: Type 'number' is not assignable to type 'T extends {} ? number : number'.
const b: (T extends { } ? number : number) = a;
~
Playground Link: Link
Use case:
I wouldn't expect anybody to write the code above. Here's something more like my original example (link):
const f = <B extends true | false>(b: B, n: number): (true extends B ? number | null : number) => {
if (b) {
return null as true extends B ? null : never;
}
return n;
};
This gives the error
error TS2322: Type 'number' is not assignable to type 'true extends B ? number : number'.
return n;
~~~~~~~~~
In this practical example, there is a workaround, changing the return type to number | (true extends B ? null : never)
. But it was hard for me to figure out what was making TypeScript unhappy, since this seemed too simple an inference for TypeScript to miss.
sehcheese, sethduncan, Methuselah96 and CharlesTaylor7
Metadata
Metadata
Assignees
Labels
DuplicateAn existing issue was already createdAn existing issue was already created