microsoft / microsoft/TypeScript
Overload resolution selects most narrow overload when `any` is involved
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: 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
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 in TypeScript playground e segui la risoluzione degli overload per le chiamate il cui tipo di argomento è any. Confronta i tipi inferiti di e2 ed e3 con le unioni di tipi restituiti previste. Il lavoro è completato quando i casi segnalati non selezionano più soltanto l’overload più specifico e i test rilevanti del compilatore coprono questo comportamento.
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