microsoft / microsoft/TypeScript

Strict version of Extract for literal union types

Aperta
#31,474 1 commento 51 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
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

extract, pick, union, literal, string

Suggestion

Currently we have Pick<T, K> to select a type-safe subset of a given type T using a union of keys K. Unfortunately this does not work for unions of literals and requires Extract which subjectively isn't strict enough:

type ShirtSizes = "xxxl" | "xxl" | "xl" | "l" | "m" | "s" | "xs" | "xxs";
type Subset = Extract<ShirtSizes, "l" | "m" | "s" | "this-is-allowed-as-well">;

One caveat is that Extract does not support code completion when writing the literal union for the keys and it allows keys not in the first type as well. The former is error-prone while the latter is not causing a different behavior since the illegal keys are ignored but it can't be strictly checked in the long run. Of course I understand there are use cases wherein you'd like to use Extract more liberally so the definition

type Extract<T, U> = T extends U ? T : never;

is reasonable enough. Nevertheless, I'd like to propose an additional strict variant as follows

// short-hand form
type ExtractStrict<T, U extends T> = Extract<T, U>;
// equivalent full form
type ExtractStrict<T, U extends T> = T extends U ? T : never;

Use Cases

type ShirtSizes = "xxxl" | "xxl" | "xl" | "l" | "m" | "s" | "xs" | "xxs";
// this allows for code completion
type Subset = ExtractStrict<ShirtSizes, "l" | "m" | "s">;
// this is causing an error
type Subset2 = ExtractStrict<ShirtSizes, "l" | "m" | "s" | "illegal-literal">;

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, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

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 esaminando il tipo utility Extract esistente e gli esempi di ShirtSizes dell'issue. Determina come una variante strict si inserirebbe nel design del sistema di tipi di TypeScript e nel comportamento dei test, inclusi il rifiuto dei literal non validi e il supporto al completamento. Il lavoro è concluso quando il tipo proposto è specificato, implementato e coperto da test del compilatore.

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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.