microsoft / microsoft/TypeScript

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

Open
#29,469 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

In Discussion Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No source files, tests, or compiler entry points are named. Start by reviewing the issue's WithStyles, Optionalize, and generic-constraint examples, then trace the TypeScript type-checking behavior for generic wrappers. Done means a wrapper can pass through the wrapped type's generic arguments and enforce its existing constraints without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.