Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typeof の説明の誤りについて #957

Open
suikan4github opened this issue Dec 25, 2024 · 0 comments
Open

Typeof の説明の誤りについて #957

suikan4github opened this issue Dec 25, 2024 · 0 comments

Comments

@suikan4github
Copy link

以下の説明は間違っていると考えます。ユニオン型は型の和集合であり、値の和集合ではありません。

2つ以上のプロパティがあるオブジェクトの型にkeyofを使った場合は、すべてのプロパティ名がユニオン型で返されます。

これはTypescript HandbookのKeyof Type Operatorを参考にしたと思われます。

The keyof operator takes an object type and produces a string or numeric literal union of its keys. The following type P is the same type as type P = "x" | "y":

しかし、ここでいうunionは「ユニオン型」ではなく「和集合」と考えるべきです。つまり、

keyof 演算子はオブジェクト型をうけとり、そのキーから文字列か数のリテラルの和集合を作ります。

と解釈すべきです。この文章はプログラム言語の解説としてはあいまいですが、「 keyof 演算子はオブジェクト型をうけとり、そのキーから文字列か数のリテラル型を作ります」と理解するのが妥当だと考えます。

ですので、冒頭の解説も

2つ以上のプロパティがあるオブジェクトの型にkeyofを使った場合は、すべてのプロパティ文字列によるリテラル型が返されます。

と変更すべきです。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant