microsoft / microsoft/TypeScript

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

Offen
#598 3 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Help Wanted Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

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.

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Beginne mit dem Vorschlag und untersuche den stopParameterNeglect-Branch einschließlich seines Codes und seiner Tests. Führe zuerst die vorhandenen Tests aus und vergleiche dann das Verhalten der Signaturkompatibilität mit der vorgeschlagenen Regel für die Parameteranzahl. Als erledigt gilt die Aufgabe, wenn Overloads, die weniger Parameter akzeptieren, nicht ausgewählt werden, wenn ein kompatibler Overload innerhalb der Arity des Callbacks mehr Parameter akzeptiert, ohne bestehende Tests zu beeinträchtigen.

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

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.