Repository navigation
Closed
Labels
BugA bug in TypeScriptA bug in TypeScript
Milestone
Description
TypeScript Version: 3.4.5
Search Terms: Discriminated Unions Type, Function, Assignment, Type Property
Code
type Foo1 = {
type: 'foo1';
extra: number;
};
type Foo2 = {
type: 'foo2';
extra: string;
};
type Both = Foo1 | Foo2;
type FooTypes = Both['type'];
export type FooFromType<T extends FooTypes, O extends Both = Both> = O extends { type: T } ? O : never;
type FooExtraFromType<T extends FooTypes> = FooFromType<T>['extra'];
function fnWithFooExtra<T extends FooTypes>(type: T, extra: FooExtraFromType<T>) { }
// changing this to typeof fnWithFooExtra fixes it, even though it produces the exact signature
type FnType = <T extends FooTypes>(type: T, extra: FooExtraFromType<T>) => void;
// The error is produced here:
const fn: FnType = fnWithFooExtra;Worked for a long time, but 3.5.1 breaks it. it hasn't been fixed on next. I tried 3.6.0-dev.20190608.
Interestingly, it breaks only on extra and not on the type itself. meaning that if I change FooExtraFromType to FooFromType it works.
If I use typeof instead of writing the signature it works. this is really weird as they are the exact same signature.
Expected behavior:
Compile as it had been working up to 3.4.5.
Actual behavior:
'fn' is declared but its value is never read.ts(6133)
Type '<T extends "foo1" | "foo2">(type: T, extra: (FooFromType<T, Foo1> | FooFromType<T, Foo2>)["extra"]) => void' is not assignable to type 'FnType'.
Types of parameters 'extra' and 'extra' are incompatible.
Type 'FooFromType<T, Foo1>["extra"] | FooFromType<T, Foo2>["extra"]' is not assignable to type 'FooFromType<T, Foo1>["extra"] & FooFromType<T, Foo2>["extra"]'.
Type 'FooFromType<T, Foo1>["extra"]' is not assignable to type 'FooFromType<T, Foo1>["extra"] & FooFromType<T, Foo2>["extra"]'.
Type 'number' is not assignable to type 'FooFromType<T, Foo1>["extra"] & FooFromType<T, Foo2>["extra"]'.
Type 'number' is not assignable to type 'FooFromType<T, Foo2>["extra"]'.
Type 'number' is not assignable to type 'string'.
Type 'FooFromType<T, Foo1>["extra"]' is not assignable to type 'FooFromType<T, Foo2>["extra"]'.
Type 'FooFromType<T, Foo1>' is not assignable to type 'FooFromType<T, Foo2>'.
Type 'FooFromType<T, Foo1>["extra"]' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.ts(2322)
Playground Link: Doesn't break on playground, I guess the version is old.
Reacted by Pouya Kary ✨, Joe Calzaretta, Armano and jp2masa
Reacted by Pouya Kary ✨
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScript
Activity
fatcerberus commentedon Jun 9, 2019
Working Playground link
Indexed access type + contravariance + inferred intersection = #30769, I'd wager.
jack-williams commentedon Jun 9, 2019
Fix up: #31837
jack-williams commentedon Jun 9, 2019
Here is a smaller repro.
Another interesting case:
jcalz commentedon Feb 21, 2021
oof, just bonked into this with code like
in this SO question
RyanCavanaugh commentedon Mar 5, 2024
No longer errors, though jcalz's still does.