Skip to content
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

Type inference: is that a bug? #58382

Closed
1 task done
jcayzac opened this issue May 1, 2024 · 3 comments
Closed
1 task done

Type inference: is that a bug? #58382

jcayzac opened this issue May 1, 2024 · 3 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@jcayzac
Copy link

jcayzac commented May 1, 2024

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

I'm not sure if the following is a bug or if it's working as intended.

interface Entry<P, D> {
  point: P
  datum: D
}

class X<P, D, E extends Entry<P, D>> {
  readonly p: P
  readonly d: D

  constructor(entry: E) {
    this.p = entry.point
    this.d = entry.datum
  }
}

const a = new X({ point: 1, datum: 'a'})

Here, a is inferred to be of type X<unknown, unknown, { point: number; datum: string; }> (while a.p and a.d have the correct types).

I expected it to be of type X<number, string, { point: number; datum: string; }>.

Can anybody explain what's going on here? I'm using Typescript 5.4.3.

@Andarist
Copy link
Contributor

Andarist commented May 1, 2024

This is just not something that TS supports. Inference targets have to appear in parameter lists - the "sources" in constraints of other type parameters are ignored. An even simpler repro:

interface Box<T> {
  value: T;
}
function test<T, B extends Box<T>>(a: B) {}
test({ value: 10 });

Arguably, you probably don't want this signature. In situations like this you probably don't want to allow such calls:

new X<number, string, { point: 1, datum: 'a' }>({ point: 1, datum: 'a' })

I'd assume that your E should be strictly { point: number, datum: string } here but your signature allows different subtypes of that.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label May 1, 2024
@jcalz
Copy link
Contributor

jcalz commented May 1, 2024

see #7234 which was not implemented in favor of having people use intersections instead.

@jcayzac
Copy link
Author

jcayzac commented May 2, 2024

Thank you both, @Andarist @jcalz

@jcayzac jcayzac closed this as completed May 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants