microsoft / microsoft/TypeScript

Suggestion: shorthand syntax for annotating function with type

Offen
#16,334 1 Kommentar 19 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

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

Beschreibung

I like to define types for my functions by separating the type out from the value:

type id = <A>(a: A) => A;
const id: id = a => a;

This helps particularly for longer functions where the parameter list often spans many columns and multiple lines.

However, compared to Elm/Haskell, this code is quite boilerplate-y. I wonder if there is some shorthand syntax we could add to make this pattern more concise. For example, the same code in Elm/Haskell:

id : a -> a
id a = a

The boilerplate in the current TypeScript syntax is:

  • Manual annotation of value (id) with type of same name (id). Could this be automatic, e.g. matched by name?
  • TypeScript requires parameter names for the function type, but are these always required? Could the function type simply by A => A, omitting the parameter names? Or would this cause problems?

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 damit, die vorgeschlagene TypeScript-Kurzschreibweise mit den Elm- und Haskell-Beispielen im Issue zu vergleichen. Definiere die akzeptierte Syntax und ihr Verhalten bei der Typprüfung, bevor du Implementierungs- und Teststellen identifizierst; das Issue gibt keine Dateien, Einstiegspunkte oder konkreten Abschlusskriterien an.

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
Muss geklärt werden
Anfängerfreundlichkeit
20/100

Neue Issues direkt in Ihr Postfach

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