microsoft / microsoft/TypeScript

Allow narrowing type based on others properties when creating a new object

オープン
#37,224 コメント 3 件 リアクション 1 件 担当者 0 名 GitHub で見る

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

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

説明

Search Terms

  • narrowing type

Suggestion

Currently, TypeScript isn't narrowing the type based on others properties that is defined while creating a new object. For example. Let's say the we have the following function:

type TParams = { kind: 'name', value: string } | { kind: 'age', value: number }
const example = (params: TParams) => {
  ...
}

So we can call it using codes like:

example({ kind: 'name', value: 'macabeus' })
example({ kind: 'age', value: 23 })

But isn't possible to compile code like:

let nameOrAge: 'name' | 'age' = 'name'

example({
    kind: nameOrAge,
    value: (nameOrAge === 'name' ? 'macabeus' : 23),
})

Playground

but we know that, if nameOrAge is 'name', the property value will be a string, otherwise nameOrAge will be 'age' and value will be 23 - that is correct.
I think that would be useful if TS could check this behaviour.

Use Cases

It would be useful to reduce the amount of code on situations like described on the above section.
Currently, is necessary to write more lines to have the same behaviour:

let nameOrAge: 'name' | 'age' = 'name'

if (nameOrAge === 'name') {    
    example({
        kind: nameOrAge,
        value: 'macabeus',
    })
} else {
    example({
        kind: nameOrAge,
        value: 23,
    })
}

Playground

Related

I know that there are a lot of issues saying about narrowing types based on variables, such as:

  • that about narrowing on arrays values
  • that about narrowing when calling a function

But here I'm saying only about when is creating a new object, and as far as I know is a simpler case if compared on these others issues, since doesn't need to track the entire code base.
On the case that I'm saying, TS just need to check the others properties defined on the same object and if the variable name already is used. For example, TS doesn't need to change its behaviour on this case:

example({
  kind: nameOrAge,
  value: otherVariabledDeclaredOnOtherScope ? 'macabeus' : 23,
}) // still should fail

Probably it isn't the best solution, but I think that it follow the Pareto efficiency.

Checklist

My suggestion meets these guidelines:

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

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

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

はじめの一歩

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

調査の方向性

まず、リンクされた TypeScript Playground の再現コードを実行し、動作するブランチの例と比較します。オブジェクトリテラルが判別可能なユニオンに対してどのようにチェックされるかを調査します。相関関係のある場合に提案された kindvalue の組み合わせが受け入れられ、無関係な変数は引き続き失敗し、コンパイラのテストスイートに回帰テストのカバレッジが追加されれば完了です。

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

評価

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

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

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