microsoft / microsoft/TypeScript

Support shorthands when specializing generic functions at the type level

Aperta
#55,204 0 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Awaiting More Feedback Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.4k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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.

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Non viene indicato alcun file sorgente TypeScript, test o punto di ingresso; inizia esaminando gli esempi abbreviati proposti e il caso d’uso grammex collegato. Il lavoro sarà completo quando si sarà raggiunto un accordo su sintassi e semantica, sarà stato implementato il supporto per le funzioni generiche specializzate nelle forme abbreviate di oggetti e tipi e sarà stata aggiunta la copertura per gli esempi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
typescript
Ambito
compilers
Tipo di issue
Funzionalità
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Ferma
Chiarezza
Abbastanza chiara
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.