microsoft / microsoft/TypeScript
Wrong overload selected on function when passed a callback annotated with a type with multiple optional properties
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.4k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
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'.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Rechercherichtung
Beginne mit der minimalen TypeScript-Reproduktion im Issue und untersuche die Überladungsauflösung für den annotierten Callback, optionale Properties und den inferierten Quelltyp. Als erledigt gilt die Aufgabe, wenn das Beispiel ohne den TS2322-Fehler kompiliert und die Callback-Überladung statt der object-iteratee-Überladung auswählt.
Vom Indexierungsmodell aus dem Issue-Text verfasst.
Bewertung
- Tech-Stack
- typescript
- Bereich
- compilers
- Issue-Typ
- Bug
- Schwierigkeit
- 4/5
- Geschätzter Aufwand
- 3-5 Tage
- Aktivitätsstatus
- Veraltet
- Klarheit
- Größtenteils klar
- Anfängerfreundlichkeit
- 42/100