microsoft / microsoft/TypeScript

Improve typing of arguments with a function (with respect to overloads)

Offen
#28,167 3 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

Search Terms

function overload arguments narrowing

Suggestion

The example is probably the best illustration I can give... It's also much clearer (I think) that the description in text.

Currently, the types of the arguments of a function with overloads cannot be forwarded to another function with the same overloads (1) because the raw typing of the argument doesn't match any of the overloads. But, at the same time, this restriction is not carried out in the relationship between the arguments' types is not used once in the function's body.

We currently have to either assert types (using as) or do if (...) throw with conditions that will never be met runtime.

That requires to sacrifice either type safety or performance.

I've seen #13225. However, my suggestion is more regarding the arguments than the return type. It shouldn't impose any additional restrictions that currently exist (actually, remove some).

Moreover, one of the arguments that was given at the time ("Pass-through overloads are extremely common and would require writing "artificial" code to satisfy the checker") is not true anymore. (See point (1) in the example). I'm not sure if this particular point should be considered a bug in itself or not...

Having the type-system aware of the overloads when typing the arguments could actually restore this.

Use Cases

Simplify overloads implementation and make them more readable.

Examples

let baz: string;

function fn2(foo: 'a', bar: string): void;
function fn2(foo: 'b', bar: number): void;
function fn2(foo: 'a' | 'b', bar: string | number): void
{
    fn(foo, bar); // (1) If this doesn't work, ...
}

function fn(foo: 'a', bar: string): void;
function fn(foo: 'b', bar: number): void;
function fn(foo: 'a' | 'b', bar: string | number): void
{
    if (foo == 'a')
    {
        baz = bar; // (2) ... This should!
    }
}

https://www.typescriptlang.org/play/index.html#src=let%20baz%3A%20string%3B%0D%0A%0D%0Afunction%20fn2(foo%3A%20'a'%2C%20bar%3A%20string)%3A%20void%3B%0D%0Afunction%20fn2(foo%3A%20'b'%2C%20bar%3A%20number)%3A%20void%3B%0D%0Afunction%20fn2(foo%3A%20'a'%20%7C%20'b'%2C%20bar%3A%20string%20%7C%20number)%3A%20void%0D%0A%7B%0D%0A%20%20%20%20fn(foo%2C%20bar)%3B%20%2F%2F%20(1)%20If%20this%20doesn't%20work%2C%20...%0D%0A%7D%0D%0A%0D%0Afunction%20fn(foo%3A%20'a'%2C%20bar%3A%20string)%3A%20void%3B%0D%0Afunction%20fn(foo%3A%20'b'%2C%20bar%3A%20number)%3A%20void%3B%0D%0Afunction%20fn(foo%3A%20'a'%20%7C%20'b'%2C%20bar%3A%20string%20%7C%20number)%3A%20void%0D%0A%7B%0D%0A%20%20%20%20if%20(foo%20%3D%3D%20'a')%0D%0A%20%20%20%20%7B%0D%0A%20%20%20%20%20%20%20%20baz%20%3D%20bar%3B%20%2F%2F%20(2)%20...%20This%20should!%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript / JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. new expression-level syntax)

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

Das Issue stellt ein TypeScript-Playground-Beispiel bereit, nennt aber keine Repository-Dateien oder Tests. Beginne damit, die Overload-Implementierung und den Weiterleitungsaufruf zu reproduzieren, und untersuche dann das Verhalten des Checkers im Zusammenhang mit Overload-Argumenten; fertig ist es, wenn der Weiterleitungsaufruf akzeptiert wird und der bedingte Zweig bar auf string eingrenzt, ohne Assertions oder unerreichbare Laufzeitprüfungen.

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.