microsoft / microsoft/TypeScript

Report better errors on return expressions for immediately-assigned functions

Offen
#42,161 6 Kommentare 2 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Domain: Error Messages Effort: Moderate Experience Enhancement Experimentation Needed Help Wanted Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
2 T. 4 Std.
Gemergte PRs (30 T.)
132

Beschreibung

Suggestion

🔍 Search Terms

return type label:"Domain: Error Messages"

✅ Viability 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. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

Error reporting for mismatches between a functions return type and what is being returned by a particular return statement should be consistently reported a the location of the offending return.

📃 Motivating Example

TypeScript Playground

type Foo = (input: string) => number;

const foo: Foo = (input) => {
  switch (input) {
    case "1":
      return 1;
    case "2":
      return "2";
    default:
      return Infinity;
  }
}

The following error is reported on the declaration of foo.

const foo: Foo
Type '(input: string) => number | "2"' is not assignable to type 'Foo'.
  Type 'number | "2"' is not assignable to type 'number'.
    Type 'string' is not assignable to type 'number'.(2322)
const bar = (input: string): number => {
  switch (input) {
    case "1":
      return 1;
    case "2":
      return "2";
    default:
      return Infinity;
  }
}

The following error is reported on return "2":

Type 'string' is not assignable to type 'number'.(2322)

💻 Use Cases

In the second example it's much easier to see where the problem is. In larger functions for more `returns it can be even hard to pin down which one is causing the issue. These two examples are very similar and I would expect the same error to be reported for each.

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 den motivierenden TypeScript-Beispielen und dem verknüpften Playground und vergleiche die Diagnosen für sofort zugewiesene Funktionen mit explizit annotierten Rückgabetypen. Das Ziel ist erreicht, wenn ein Rückgabetypfehler in der ersten Form an der fehlerhaften return-Anweisung gemeldet wird, konsistent mit dem zweiten Beispiel, ohne das erzeugte JavaScript zu ändern.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
typescript
Bereich
compilers
Issue-Typ
Feature
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
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.