-
Notifications
You must be signed in to change notification settings - Fork 13.2k
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
π Search Terms
"infer"
π Version & Regression Information
- This changed in commit e1874f3
β― Playground Link
π» Code
export class Test<A, B> {
constructor (public a: A, public b: B) {}
test<C>(c: C): Test<B, C> {
return new Test(this.b, c);
}
}π Actual behavior
The invocation of new Test infers generic parameters of <C, C>, even though this.b is B and c is C, which should pretty clearly result in <B, C>. This incorrect inference causes an error on return since it does not match the return type annotation:
Type 'Test<C, C>' is not assignable to type 'Test<B, C>'.
Type 'C' is not assignable to type 'B'.
'B' could be instantiated with an arbitrary type which could be unrelated to 'C'.(2322)
Explicitly invoking new Test<B, C> compiles without error.
π Expected behavior
Compiles successfully.
Additional information about the issue
I initially posted this with a slightly more complex example, where the second argument to both Testβs constructor and the test method was a callback, (v: A) => B and (v: B) => C, respectively. I realized that this callback was a bit of a red herring and the bug occurs even when these are simple, explicitly-annotated values.
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue