microsoft / microsoft/TypeScript

Overload resolution selects most narrow overload when `any` is involved

Abierto
#39,833 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Bug Domain: check: Type Inference
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
1 d 19 h
PR fusionados (30 d)
117

Descripción

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

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Comienza con la reproducción en TypeScript playground y sigue la resolución de sobrecargas para las llamadas cuyo tipo de argumento es any. Compara los tipos inferidos de e2 y e3 con las uniones de tipos de retorno esperadas. Se considera terminado cuando los casos informados ya no seleccionen únicamente la sobrecarga más específica y las pruebas relevantes del compilador cubran este comportamiento.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
typescript
Área
compilers
Tipo de issue
Error
Dificultad
4/5
Tiempo estimado
3-5 días
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
35/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.