Skip to content

Conditional types cannot infer args array #23229

Closed

Description

@tyrielv

TypeScript Version: 2.9.0-dev.20180406

Search Terms:
conditional type infer array parameter
Code

// This works
type PromisifyWithOneArg<T> = {
    [P in keyof T]?: T[P] extends (arg: infer R1) => infer R2 ? (args: R1) => Promise<R2> : T[P];
};
// This doesn't
type Promisify<T> = {
    [P in keyof T]?: T[P] extends (...args: infer R1) => infer R2 ? (...args: R1) => Promise<R2> : T[P];
};

Expected behavior:
These type definitions work, allowing definition of a generic type that makes all methods in an interface return promises, while preserving the type constraints of the arguments.
Actual behavior:
The ...args version is rejected. I either have to define N versions of the conditional type with different numbers of arguments, or lose type safety for the arguments.

Playground Link:

Related Issues:

Activity

  1. mhegazy commented on Apr 6, 2018

    @mhegazy
    Contributor

    rest parameters need to be of array type...

    type Promisify<T> = {
        [P in keyof T]?: T[P] extends (...args: Array<infer R1>) => infer R2 ? (...args: R1[]) => Promise<R2> : T[P];
    };

    general issue of unifying parameters and arrays/tuples is #5453

  2. typescript-bot commented on Apr 21, 2018

    @typescript-bot
    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  3. locked and limited conversation to collaborators on Jul 30, 2018
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

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions