microsoft / microsoft/TypeScript

Support shorthands when specializing generic functions at the type level

Offen
#55,204 0 Kommentare 1 Reaktion 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

Awaiting More Feedback Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.4k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

Suggestion

🔍 Search Terms

  • shorthands
  • generic
  • specialization

✅ Viability Checklist

  • 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

Basically support for syntax like this:

const someObject = {
  foo<number>,
  bar<string>
};

One can already specialize a generics cheaply like this:

const specializedFoo = foo<number>;

But this kind of specialization doesn't work well with shorthand syntax, so rather than writing what I suggested one has to write the more verbose:

const someObject = {
 foo: foo<string>,
 bar: bar<number>
};

Basically imo this convenience feature for specializing a generic should compose with other existing convenience syntax.

📃 Motivating Example

I have a little parser generator, problem is for many grammars the generic <T> type that most exported composable functions accept is inferred in incompatible ways between usages of those composable functions throughout a grammar, so having a way to return specialized functions, where the value of the type has been already been provided consistently for all those functions, would simplify some scenarios a lot.

But I can't write something like this:

const grammar = <T, U> ( fn: ({ match: match<T>, and: and<T>, ... }) => U ): U => {
	return fn ({
        match<T>,
        and<T>,
        ...
    });
};

I have to write this:

const grammar = <T, U> ( fn: ({ match: match<T>, and: and<T>, ... }) => U ): U => {
	return fn ({
        match: match<T>,
        and: and<T>,
        ...
    });
};

Arguably this kind of syntax should be supported for types also, so that I could even just write this, ideally:

const grammar = <T, U> ( fn: ({ match<T>, and<T>, ... }) => U ): U => {
	return fn ({
        match<T>,
        and<T>,
        ...
    });
};

💻 Use Cases

Simplifying the code in the example above.

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

Es wird keine TypeScript-Quelldatei, kein Test und kein Einstiegspunkt genannt; beginne mit der Prüfung der vorgeschlagenen Kurzbeispiele und des verknüpften grammex-Anwendungsfalls. Erledigt wäre dies, wenn eine Einigung über Syntax und Semantik erzielt, die Unterstützung für spezialisierte generische Funktionen in Objekt- und Typ-Kurzformen implementiert und eine Abdeckung für die Beispiele hinzugefügt wurde.

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

Neue Issues direkt in Ihr Postfach

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