Skip to content

Second infer overwrites initally infered type defintion #32389

Closed

Description

TypeScript Version: 3.5.1

Search Terms:

Infer, Multiple, Generic

Code

type RequesterBaseType = (...args: any[]) => any;

type SuccessUnpacker<RequesterType extends RequesterBaseType, PayloadType> = (
  payload: ReturnType<RequesterType>
) => PayloadType;


type ResultTracker<PayloadType> = PayloadType extends ArrayLike<infer PackedPayloadType>
  ? (instance: PackedPayloadType) => Partial<PackedPayloadType>
  : (instance: PayloadType) => Partial<PayloadType>;

declare function createApiModule<
  PayloadType,
  TrackingType,
  RequesterType extends RequesterBaseType = RequesterBaseType
>(
  apiMethod: RequesterType,
  unpackSuccess: SuccessUnpacker<RequesterType, PayloadType>,
  trackResult?: ResultTracker<PayloadType>
): void;

type MyPayload = {
    a: string;
}
createApiModule(
   () => [{a: 'a'}] as MyPayload[],
  payload => payload,
  ({ a }) => ({ a })
);

Expected behavior:

Allowed return type of unpackSuccess should be as MyPayload[]

Actual behavior:

Allowed return type of unpackSuccess is be MyPayload.

Playground Link

Related Issues:


I tried to search for related issues but could not find anything, maybe I don't know the correct terminology.

Activity

  1. clentfort commented on Jul 16, 2019

    @clentfort
    Author

    I found a workaround that at least for now solves my problem by changing Infer2 to the following

    type Infer2<T> = T extends ArrayLike<infer S>
      ? <SS extends S>(s: SS) => Partial<SS>
      : <TT extends T>(t: TT) => Partial<TT>;

    I'm not sure if this could have undesired side-effects, but at least it allows me to continue.

  2. dragomirtitian commented on Jul 16, 2019

    @dragomirtitian
    Contributor

    Christian Lentfort (@clentfort) If you want a workaround using & {} will decrease the priority of the second inference site:

    type Infer1<T> = () => T;
    type Infer2<T> = T extends ArrayLike<infer S>
      ? (a: S) => Partial<S>
      : (a: T) => Partial<T>;
    
    declare function f<T>(infer1: Infer1<T>, infer2: Infer2<T & {}>): void;
    declare function f1(): Array<{ a: any }>;
    
    f(f1, a => a);

    link

    Your workaround makes the second parameter a generic function, which will behave differently. For example this would not be legal:

    type Infer1<T> = () => T;
    type Infer2<T> = T extends ArrayLike<infer S>
      ? <SS extends S>(s: SS) => Partial<SS>
      : <TT extends T>(t: TT) => Partial<TT>;
    
    declare function f<T>(infer1: Infer1<T>, infer2: Infer2<T>): void;
    declare function f1(): { a: any };
    
    f(f1, (s) => ({ a: 1 })); // concrete value assigned to a generic type not allowed.
  3. clentfort commented on Jul 16, 2019

    @clentfort
    Author

    Thanks for pointing this out, this means my workaround is exactly what I want. :)

  4. RyanCavanaugh commented on Jul 31, 2019

    @RyanCavanaugh
    Member

    See #30134 - the particular use of depending on an infer in a conditional type simultaneously while doing an outer generic inference isn't well-supported in the current algorithm.

  5. added
    Design Limitation
    on Jul 31, 2019
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

    Design Limitation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions