Skip to content

Class infers wrong generic parameter since e1874f3 #58074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
krryan opened this issue Apr 4, 2024 · 2 comments Β· Fixed by #58168
Closed

Class infers wrong generic parameter since e1874f3 #58074

krryan opened this issue Apr 4, 2024 · 2 comments Β· Fixed by #58168
Assignees
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue

Comments

@krryan
Copy link

krryan commented Apr 4, 2024

πŸ”Ž Search Terms

"infer"

πŸ•— Version & Regression Information

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.5.0-dev.20240404#code/KYDwDg9gTgLgBAYwDYEMDOa4BVhpgHgEEAaOAIQD44BvAKDgcQgDs8oBXBGaOACjHYAjJAEsEcFAC44JOAOFi4g6WQCUNAL616jGLgIBhCrwTSDq6Tjz4ypIzR2NGUYDHZRmcZsADu2fbwwABYiaAB0gqQIqgDcjnBaWkA

πŸ’» 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.

@krryan krryan changed the title Class infers wrong generic parameter in latest (regression?) Class infers wrong generic parameter since e1874f3 Apr 4, 2024
@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Apr 4, 2024
@RyanCavanaugh RyanCavanaugh added this to the TypeScript 5.5.0 milestone Apr 4, 2024
@Andarist
Copy link
Contributor

Andarist commented Apr 8, 2024

Extra repro for what I believe to be the same problem (cc @mikearnaldi):

interface Supervisor<out T> {
  zip<A>(right: Supervisor<A>): Supervisor<[T, A]>;
}

export class Zip<out T0, out T1> implements Supervisor<readonly [T0, T1]> {
  constructor(
    readonly left: Supervisor<T0>,
    readonly right: Supervisor<T1>,
  ) {}

  zip<A>(right: Supervisor<A>): Supervisor<[[T0, T1], A]> {
    return new Zip(this, right);
  }
}

@weswigham
Copy link
Member

#58168 is up with a fix - just need to expand a scope we're looking at from the constructor signature to the whole class body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Fix Available A PR has been opened for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants