microsoft / microsoft/TypeScript
Overload resolution selects most narrow overload when `any` is involved
Dieses Issue hat noch niemand übernommen.
- Vorherrschende Sprache
- Go
- Sterne
- 111k
- Forks
- 14.3k
- Ø Merge
- 1 T. 19 Std.
- Gemergte PRs (30 T.)
- 117
Beschreibung
TypeScript Version: 4.0.0-dev.20200730
Search Terms: any overload
Code
declare class Endpoint {}
declare function getEndpoint(index: number): Endpoint | undefined;
declare function getEndpoint(index: 0): Endpoint;
declare const i1: number | undefined;
const e1 = getEndpoint(i1 ?? 0); // Endpoint | undefined, as expected
declare const i2: any;
const e2 = getEndpoint(i2); // Endpoint, expected Endpoint | undefined
Expected behavior:
The type of e2 should include undefined, since we cannot know that the most narrow overload always matches.
Actual behavior:
It does not.
I understand that it is not always possible to select a sound overload, especially when the possible argument types are not strictly subsets of each other. However choosing the most narrow overload does not seem correct.
Take this extension of the repro for example:
declare class Endpoint {}
declare function getEndpoint(index: number): Endpoint | undefined;
declare function getEndpoint(index: 1 | 2): "FOOBAR";
declare function getEndpoint(index: 0): Endpoint;
declare const i3: any;
const e3 = getEndpoint(i3);
Here, e3 is inferred as Endpoint. When I change the argument in the 2nd overload to 1 instead of 1 | 2, e3 suddenly has type "FOOBAR". I would argue that it is not possible to narrow to any overload here, since i3 could be any number (or even any other type).
Therefore I believe that the most sound result would be the union of all return types, that is Endpoint | undefined | "FOOBAR".
Playground Link: Playground Link
Related Issues: none found that seems to match this one
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 Reproduktion im TypeScript playground und verfolge die Überladungsauflösung für Aufrufe, deren Argumenttyp any ist. Vergleiche die inferierten Typen von e2 und e3 mit den erwarteten Vereinigungen der Rückgabetypen. Als erledigt gilt die Aufgabe, wenn die gemeldeten Fälle nicht mehr ausschließlich die spezifischste Überladung auswählen und relevante Compiler-Tests dieses Verhalten abdecken.
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
- 35/100