microsoft / microsoft/TypeScript

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

オープン
#29,469 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

In Discussion Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
1日 19時間
マージ済み PR(30日)
117

説明

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

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

ソースファイル、テスト、またはコンパイラのエントリポイントは指定されていません。まず issue の WithStyles、Optionalize、およびジェネリック制約の例を確認し、次にジェネリックラッパーに対する TypeScript の型チェック動作を追跡してください。ラッパーがラップされた型のジェネリック引数をそのまま渡し、既存の制約を適用でき、生成される JavaScript を変更しなければ完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
typescript
領域
compilers
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。