microsoft / microsoft/TypeScript

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

Offen
#29,469 2 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

In Discussion Suggestion
Vorherrschende Sprache
Go
Sterne
111k
Forks
14.3k
Ø Merge
1 T. 19 Std.
Gemergte PRs (30 T.)
117

Beschreibung

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

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 werden keine Quelldateien, Tests oder Compiler-Einstiegspunkte genannt. Beginne damit, die Beispiele für WithStyles, Optionalize und generische Constraints im Issue zu prüfen, und verfolge anschließend das TypeScript-Typprüfverhalten für generische Wrapper. Das Ziel ist erreicht, wenn ein Wrapper die generischen Argumente des umschlossenen Typs durchreichen und seine bestehenden Constraints durchsetzen kann, ohne das erzeugte JavaScript zu verändern.

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.