microsoft / microsoft/TypeScript

TypeScript does not infer the type correctly after two layer Mapped Types.

Open
#38,001 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.9.0-beta

Search Terms:

Lookup Types, Mapped Types

Code

This is Okay:

interface APIs {
  readonly getUserProfile: {
    readonly Parameter: {
      readonly id: string;
    };
    readonly Response: {
      '200': {
        readonly id: string;
        readonly name: string;
        readonly gender: boolean;
      };
    };
  };
}

export type ValueType<T extends object> = T[keyof T];

export type PropertyType<T extends object, K extends string> = K extends keyof T
  ? T[K]
  : never;

type MyFetch = {
  [operationId in keyof APIs]: (
    param: PropertyType<APIs[operationId], 'Parameter'>,
  ) => Promise<
    ValueType<PropertyType<APIs[operationId], 'Response'>>
  >;
};

And this is Error: Type 'APIs[server][string]' is not assignable to type 'object'


interface APIs {
  readonly server1: {
    readonly getUserProfile: {
      readonly Parameter: {
        readonly id: string;
      };
      readonly Response: {
        '200': {
          readonly id: string;
          readonly name: string;
          readonly gender: boolean;
        };
      };
    };
  };
}

export type ValueType<T extends object> = T[keyof T];

export type PropertyType<T extends object, K extends string> = K extends keyof T
  ? T[K]
  : never;

type MyFetch = {
  [server in keyof APIs]: {
    [operationId in keyof APIs[server]]: (
      param: PropertyType<APIs[server][operationId], 'Parameter'>,
    ) => Promise<
      ValueType<PropertyType<APIs[server][operationId], 'Response'>>
    >;
  };
};

And this is Ok

interface APIs {
  readonly server1: {
    readonly getUserProfile: {
      readonly Parameter: {
        readonly id: string;
      };
      readonly Response: {
        '200': {
          readonly id: string;
          readonly name: string;
          readonly gender: boolean;
        };
      };
    };
  };
}

export type ValueType<T extends object> = T[keyof T];

export type PropertyType<T extends object, K extends string> = K extends keyof T
  ? T[K]
  : never;

type MyFetch = {
  [server in keyof APIs]: {
    [operationId in keyof APIs[server]]: (
      param: PropertyType<{} & APIs[server][operationId], 'Parameter'>,
    ) => Promise<
      ValueType<{} & PropertyType<{} & APIs[server][operationId], 'Response'>>
    >;
  };
};

Expected behavior:

remove three {} & and no error.
Actual behavior:

Playground Link:

Ok Playground Link 0

Error Playground Link

Ok Playground Link

Related Issues:

https://github.com/pirix-gh/ts-toolbelt/issues/107

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by running the Error Playground Link with TypeScript 3.9.0-beta and compare it with the two Ok Playground Links. Investigate the mapped-type and lookup-type inference path responsible for the reported constraint error. Done means the three {} intersections can be removed without an error in the reproduced example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.