Skip to content

Weird behavior with generics in generic tuple types #41857

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
maxludovicohofer opened this issue Dec 7, 2020 · 1 comment
Closed

Weird behavior with generics in generic tuple types #41857

maxludovicohofer opened this issue Dec 7, 2020 · 1 comment
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@maxludovicohofer
Copy link

TypeScript Version: 4.1.2

Search Terms:
Tuple generics
Generics in function tuple types

Code

type Act<S extends object> = {
  act<I extends ((shape: S) => object)[]>(
    ...intentions: I
  ): Act<S> & S & UnionToIntersection<ReturnType<I[number]>>;
};

Expected behavior:
Once S is provided, The 'act' function should automatically infer the specific type of S, which is constant for every element of the tuple type (I), and make the 'shape' parameter typesafe.
This behavior works correctly when:

// Without tuple type
type Act<S extends object> = {
  act<I extends object>(
    ...intentions: ((shape: S) => I)[]
  ): Act<S> & S & I;
};

Actual behavior:
S is inferred as object, instead of its specific type. The return type is inferred correctly.

Playground Link: https://www.typescriptlang.org/play?#code/C4TwDgpgBAqgdgSwPZwCpIJJ2BATgZwgGNhk4AeGAPigF4oAKGKCADxzgBN8oBDOEFAD8jMAC5YASjo0AbkgScoEuBFl5pbDt0YAoKFHFQEcAGZ4oGXdNpyFnfcMuOVavAG5Hu0JCgBBEnIAZRZ2CC4eJAAjACtiYBp6AG9HXkCMUO0eBgZ8AAteSAkgmxpouJJJAG0AXSoGRwMAOhaTDlIUfAkrA0kJAOBgmgAyKBDR+DJ0LBwCeLJyACUIYABXXDRwCHIMKrhVgFsovDqqTwBfdyA

Related Issues:

@RyanCavanaugh RyanCavanaugh added the Needs More Info The issue still hasn't been fully clarified label Dec 7, 2020
@RyanCavanaugh
Copy link
Member

We don't support anything that involves UnionToIntersection; this is an ill-defined and will frequently produce "surprising" results.

I don't understand what you mean by "S is inferred as object" - it's just a type parameter. It sounds like you're referring to some specific instantiation of Act but there's just declarations in the repro?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

2 participants