microsoft / microsoft/TypeScript
Wrong overload selected on function when passed a callback annotated with a type with multiple optional properties
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 2g 4h
- PR unite (30g)
- 132
Descrizione
TypeScript Version: 2.7.0-dev.20171128
Code
This issue was first noticed in code that uses LoDash with the typings from DefinitelyTyped. It seems to manifest only in a very specific set of circumstances. The following code sample is as minimal a reproduction as I could find.
let _: LoDashStatic;
interface LoDashStatic {
mapValues<T extends object, TResult>(
obj: T,
callback: (value: T[keyof T]) => TResult
): { [P in keyof T]: TResult };
mapValues<T extends object>(obj: T, iteratee: object): { [P in keyof T]: boolean };
}
interface Item {
key?: string;
bar?: number;
}
interface Collection {
[key: string]: Item;
}
const source = { foo: { bar: 42 } };
let items: Collection = _.mapValues(
source,
(v: Item) => v
);
Note that the correct overload is selected when any of the following changes are made to the above code:
- Remove the type annotation on the callback:
(v) => vinstead of(v: Item) => v - Remove the nested optional property:
{ foo: {} }instead of{ foo: { bar: 42 } } - Remove the unused optional property:
interface Item { bar?: number; }instead ofinterface Item { key?: string; bar?: number; } - Annotate the source collection:
const source: Collection = { foo: { bar: 42 } };instead ofconst source = { foo: { bar: 42 } };
Expected behavior:
Compiles without issue.
Actual behavior:
error TS2322: Type '{ foo: boolean; }' is not assignable to type 'Collection'.
Property 'foo' is incompatible with index signature.
Type 'boolean' is not assignable to type 'Item'.
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 con la riproduzione minima in TypeScript nell’issue e analizza la risoluzione degli overload per il callback annotato, le proprietà opzionali e il tipo sorgente inferito. Il lavoro è completato quando l’esempio compila senza l’errore TS2322 e seleziona l’overload del callback anziché l’overload object-iteratee.
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
- 42/100