microsoft / microsoft/TypeScript

Supply label names in tuple types using type-level strings

Aperta
#56,093 6 commenti 9 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

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

Descrizione

🔍 Search Terms

Labelled tuples

✅ 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 our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion

It's already possible to manually specify labels when defining a tuple type:

type MyTuple = [foo: number, bar: string];

I propose that there be some syntax to allow these labels to be supplied explicitly in mapped types, eg.

type Labels = ["foo", "bar"]
type Values = [number, string]
type MyTuple = {
    // Hypothetical syntax
    [I in keyof Values]: Values[I] as Labels[I]
}

To be clear: there is still no way to "access" labels of tuples under this proposal. It's purely a way to add extra documentation into existing type definitions.

If the provided name can be determined to be a literal string type, then the value of that literal string is used as the tuple element name. In all other cases, the element is unnamed as before.

📃 Motivating Example

There are many cases where the developer can supply useful names, such as if the tuple is somehow derived from an object type, or similar.

My particular case involves taking a user-supplied array like the following:

const path = ["examples", param("exampleId"), "line", param<number>("lineNumber")] as const;

And I use this constant to generate a type for paths, which in this case would look like:

type Path = TypeMagic<typeof path>;
// type Path = ["examples", string, "line", number]

It would be nicer if I could generate this type instead:

type Path = TypeMagic<typeof path>;
// type Path = ["examples", exampleId: string, "line", lineNumber: number]
💻 Use Cases

This came up while integrating TanStack Query into an application.

Since this is purely a documentation improvement, the workaround for the moment is to simply do without labels.

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

Inizia con l’esempio di mapped type e la trasformazione motivante TypeMagic<typeof path> descritta nell’issue. Determina come i tipi stringa letterali potrebbero diventare etichette degli elementi delle tuple, mentre i nomi non letterali rimangono senza nome; il completamento consiste nell’avere una sintassi documentata e concordata e il comportamento corrispondente del sistema di tipi, senza modifiche a runtime.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.