Type Challenges Judge

Permutation

提出詳細

// [T]しておかないとdistributeされてneverのケースが実行されない // https://www.typescriptlang.org/docs/handbook/2/conditional-types.html#:~:text=Typically%2C%20distributivity%20is%20the%20desired%20behavior.%20To%20avoid%20that%20behavior%2C%20you%20can%20surround%20each%20side%20of%20the%20extends%20keyword%20with%20square%20brackets. // K extends Kとすることでunionの全組み合わせを走査できる。forを回しているようなイメージ type Permutation<T, K = T> = [T] extends [never] ? [] : K extends K ? [K, ...Permutation<Exclude<T, K>>] : never
提出日時2023-05-08 01:36:09
問題Permutation
ユーザーDowanna
ステータスAccepted
テストケース
import type { Equal, Expect } from '@type-challenges/utils' type cases = [ Expect<Equal<Permutation<'A'>, ['A']>>, Expect<Equal<Permutation<'A' | 'B' | 'C'>, ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']>>, Expect<Equal<Permutation<'B' | 'A' | 'C'>, ['A', 'B', 'C'] | ['A', 'C', 'B'] | ['B', 'A', 'C'] | ['B', 'C', 'A'] | ['C', 'A', 'B'] | ['C', 'B', 'A']>>, Expect<Equal<Permutation<never>, []>>, ]