Skip to content

Weird behavior with generics in generic tuple types #41857

Closed

Description

@maxludovicohofer

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:

Activity

  1. RyanCavanaugh commented on Dec 7, 2020

    @RyanCavanaugh
    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Needs More InfoThe issue still hasn't been fully clarified

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions