microsoft / microsoft/TypeScript

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

Aperta
#38,001 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Needs Investigation
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia eseguendo l'Error Playground Link con TypeScript 3.9.0-beta e confrontalo con i due Ok Playground Links. Analizza il percorso di inferenza di mapped-type e lookup-type responsabile dell'errore di constraint segnalato. È completato quando le tre intersezioni {} possono essere rimosse senza errori nell'esempio riprodotto.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Bug
Difficoltà
4/5
Tempo stimato
3-5 giorni
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
35/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.