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

「ユニオン分配 (union distribution)」 ページの解説について #955

Open
ykrods opened this issue Dec 18, 2024 · 0 comments
Open

Comments

@ykrods
Copy link

ykrods commented Dec 18, 2024

該当ページ

内容

Distributive Conditional Types によると

When conditional types act on a generic type, they become distributive when given a union type.

とあり、ユニオンの分配は条件型( Conditional Types )の型引数にユニオン型が与えられた場合でのみ発生するように読めます。実際の挙動でも条件型でない ToArray2<T> では分配が行われません。

type ToArray1<T> = T extends unknown ? T[] : never
type ToArray2<T> = T[]

type TA1 = ToArray1<number | string> // 結果: string[] | number[]
type TA2 = ToArray2<number | string> // 結果: (string | number)[]

const ta1: TA1 = [1, "2"]  // error
const ta2: TA2 = [1, "2"]  // OK

このことから、該当記事の

type Wrapper<T> = {
  value: T;
};

に対する

予想通りの結果が出ますが、実はこれはユニオン分配によって Wrapper<number> | Wrapper<string> として評価されたあとの結果を示しています。

という記述は適切でないと思います。

付け加えると、「ユニオン分配」という用語自体が「型引数がユニオンの場合に常に分配されるもの」という印象を与えてしまうので Distributive Conditional Types をそのまま訳すか、英語のままにした方が良いように思えます。(訳すのであれば「条件型の分配(性)」「分配的条件型」など?

@ykrods ykrods changed the title ユニオン分配 ページに関して 「ユニオン分配 (union distribution)」 ページの解説について Dec 18, 2024
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