microsoft / microsoft/TypeScript
TypeScript does not infer the type correctly after two layer Mapped Types.
Nessuno ha ancora preso questa issue.
- 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:
Related Issues:
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- 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