microsoft / microsoft/TypeScript

`// @ts-mutate` decorator/directive

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

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

Awaiting More Feedback Suggestion
主要言語
Go
スター
111k
フォーク
14.3k
平均マージ
2日 4時間
マージ済み PR(30日)
132

説明

### 🔍 Search Terms

mutate type, alter type, post-declaration narrow

### ✅ Viability Checklist

- [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
- [x] This wouldn't change the runtime behavior of existing JavaScript code
- [x] This could be implemented without emitting different JS based on the types of the expressions
- [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

### ⭐ Suggestion

When doing property assignment to a variable already typed, allow a `// @ts-mutate` above the assignment which will
- A) Not throw an error trying to assign to a non-existent property
- B) Further narrow the type, encompassing the change

Example:
```ts
interface ObjectType {
bar: string
}

const foo: ObjectType = { bar: 'hello' }

// @ts-mutate
foo.baz = 'hello'

foo
// ^? -- this is now `ObjectType & { baz: string }`
```

### 📃 Motivating Example

One specific example of why this would be useful would be a query constructor for type inference. There are GraphQL query constructors out there ([GenQL](https://genql.dev/)) that query by supplying an object. I've built functions that yield reusable queries but sometimes I want to extend them.

This would allow me to take an existing query object and add an additional field to query, keeping my strict type inference.

### 💻 Use Cases

1. What do you want to use this for?
> This could be used for constructing large objects sequentially and with inference without having to basically duplicate the information
2. What shortcomings exist with current approaches?
> If you have an existing variable, you have to alter its type declaration with the type that it is (explicitly) and intersect it with your changes. This also doesn't necessarily narrow correctly. There's no exisiting clean, easy way to do this that works off of type inference and narrowing
3. What workarounds are you using in the meantime?
> ```ts
> // UGLY
> const query: ReturnType & { prop: { addition?: true } } = queryProp(...args)
> query.prop.addition = true
> ```

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

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

はじめの一歩

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

調査の方向性

この issue では、実装ファイル、テスト、エントリポイントが特定されていません。まず、提案されている `@ts-mutate` の動作と、代入チェックおよび型の絞り込みとの相互作用を確認してください。設計が受け入れられ、要求された推論動作がテストでカバーされて初めて、作業は完了です。

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

評価

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

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

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