microsoft / microsoft/TypeScript

Parameter type narrowing should consider function overload signatures

Offen
#14,515 4 Kommentare 15 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

TypeScript Version: 2.2.1

Code

interface A { x: number; }
interface B { y: number; }

function foo(b: B): void;
function foo(a: A, b: B): void;
function foo(a: A|B, b?: B): void {
  if(b !== void 0) {
    console.log(a.y); // Error: Property 'y' does not exist on type 'A | B'
  }
}

The third (unified) function signature is hidden from the call site, and therefore any arguments should be able to be narrowed based on inspection of the provided arguments. In the case above, the second argument is only defined if the first argument is of type A, and so I would expect narrowing of the argument type to occur accordingly. One could make the argument that in the compiled output, it's possible for JavaScript to still pass subsequent arguments, but then why narrow types at all, seeing as JavaScript can always violate every type constraint that TypeScript makes inferences from?

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 der TypeScript 2.2.1-Reproduktion im Issue und untersuche, wie Überladungssignaturen während der Parametereingrenzung mit der vereinheitlichten Implementierungssignatur zusammenhängen. Überprüfe die aktuelle Diagnose für a.y und ermittle dann, ob das gewünschte Verhalten mit den angegebenen Überladungen übereinstimmt. Die Aufgabe ist abgeschlossen, wenn das Beispiel das Argument wie erwartet eingrenzt, ohne nicht zugehörige Typprüfungen abzuschwächen.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
5/5
Geschätzter Aufwand
Über eine Woche
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

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