microsoft / microsoft/TypeScript

Don't allow falsely discarded parameters when determining overloaded signature compatibility

Abierto
#598 3 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Help Wanted Suggestion
Lenguaje dominante
Go
Estrellas
111k
Forks
14.3k
Merge medio
2 d 4 h
PR fusionados (30 d)
132

Descripción

Motivation

Libraries such as Knockout use overloaded functions, for example:

var x = new MyKnockoutModel();
console.log(x.customerName()); // string
x.customerName('bob'); // OK
x.customerName(42); // Error, 42 is not a string

We can model these in TypeScript:

interface KnockoutElement<T> {
  // Getter
  (): T;
  // Setter
  (value: T): void;
}

However, these overloads turn out to not be safe when used in callbacks. For example:

function readFile(finished: (contents: string) => void) { /*... */ }
var ko: KnockoutElement<number>;
readFile(ko); // No error, but passes a string to a number-expecting setter!

This is because we look at all the candidate source signatures, reject the (x :number) => void signature for having an incompatible type, and accept the () => T signature because it has fewer parameters (an acceptable variance).

This behavior does not map to any actual runtime behavior. The invoker's side might check the .length property of the function to determine what kind of arguments to pass, and the invokee's side might check arguments.length to determine which overload was intended, but the invoker cannot tell what the expected parameter type of the function is, and the invokee reasonably expects to be called with the correct type given a certain arity.

Proposal

As an example, if a callback declaration says it’s going to invoke f with two parameters, we should not consider overloads of f which take zero arguments if an overload with one or two arguments exists.

In 3.8.3 / 3.8.4:

M is a non-specialized call or construct signature and S’ contains a call or construct signature N where […]

Change to

M is a non-specialized call or construct signature and S’ contains a call or construct signature N where no other call or construct signature in S’ has more parameters than N but not more than M, and […]

Analysis

A sample implementation of this rule breaks no existing tests. Check stopParameterNeglect branch for code/tests.

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 propuesta e inspecciona la rama stopParameterNeglect, incluido su código y sus pruebas. Ejecuta primero las pruebas existentes y, después, compara el comportamiento de compatibilidad de firmas con la regla propuesta para el número de parámetros. Se considera terminado cuando no se seleccionan overloads que aceptan menos parámetros si existe un overload compatible que acepta más parámetros dentro de la aridad del callback, sin romper las pruebas existentes.

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
48/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.