microsoft / microsoft/TypeScript

[feature request] not sure how to word this: "infer generic arg type/constraint in wrapping type"

Aperta
#29,469 2 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

In Discussion Suggestion
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
2g 4h
PR unite (30g)
132

Descrizione

Search Terms

Suggestion

Sometimes we want to wrap an existing type, where the existing type has generic type args that have constraints using extends.

Use Cases

For example, this is material-ui's WithStyles type:


and I can wrap it like this to make one of it's properties optional:

// omit a set of keys from a type
type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>

// like Partial, but choose which keys are optional instead of making all of them optional
type Optionalize<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>

// my wrapper type:
export type WithStyles_OptionalClasses<
  Style extends string | StyleRules | StyleRulesCallback = string,
  IncludeTheme extends boolean | undefined = false
> = Optionalize<WithStyles<Style, IncludeTheme>, 'classes'>

Examples

But it'd be nice if we could just pass along the args, not having to re-define them. Maybe it would look like the following, and achieve the same thing:

export type WithStyles_OptionalClasses<Style, IncludeTheme> =
  Optionalize<WithStyles<Style, IncludeTheme>, 'classes'>

and it would take advantage of the fact that WithStyles already has the constraint, so if we pass something into WithStyles_OptionalClasses that doesn't meet the constraint defined by WithStyles, then it could throw an error just like when using WithStyles by itself.

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code not sure
  • 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. not sure

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 vengono indicati file sorgente, test o punti di ingresso del compilatore. Inizia esaminando gli esempi dell’issue relativi a WithStyles, Optionalize e ai vincoli generici, quindi traccia il comportamento del controllo dei tipi di TypeScript per i wrapper generici. Il lavoro è completato quando un wrapper può trasferire gli argomenti generici del tipo racchiuso e far rispettare i vincoli esistenti senza modificare il JavaScript emesso.

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.