Skip to content

Second infer overwrites initally infered type defintion #32389

Closed
@clentfort

Description

@clentfort

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions