IssueRaiser

296 - Permutation (with explanations)

## Answer [Playground Link](https://www.typescriptlang.org/play?#code/C4TwDgpgBAChBOBbArsAhsAlgewHYB4AVAGigGkBeQgPigoCgomoBtQgXSggA9gJcAJgGdWuCADcE7RsyYB+VtNlMAXOS69+w8jOVQFLMqQB0puElQYcBAKLcAxgBtkAiEVJlq1JXqhqxkvD09KCQsAgofAJ04RboWHj4AORoSVAAPlBJAEZJtFAA9AWsKUmkOUmcmSwVGVmp7EA) (TypeScript 4.1+ only) ```ts type Permutation<T, K=T> = [T] extends [never] ? [] : K extends K ? [K, ...Permutation<Exclude<T, K>>] : never type Permuted = Permutation<'a' | 'b'> // ['a', 'b'] | ['b' | 'a'] ``` Whoa...that is weird looking. Don't worry I'll break it all down; weird piece, by weird piece 😆 <details> <summary>TLDR by @mistlog (Click me)</summary> > https://github.com/type-challenges/type-challenges/issues/614#issuecomment-790210311 </details> <details> <summary>Excellent Chinese translation by @zhaoyao91 (Click me)</summary> > https://github.com/type-challenges/type-challenges/issues/614#issuecomment-1312632631 </details> ## Explanation ### `[T] extends [never]` What in the bowels of 16-bit hell is that? Glad you asked. That my friends is a "feature" of TypeScript. It makes sense _eventually_, so just bear with me. Imagine you want to make a function called: `assertNever`. This function would be used for checking to make sure a type is...well...`never`. Pretty simple concept. You might want to use it to check some weird types you are building or something. Just roll with it, OK? <details> <summary>Wanna know a juicy secret? (Click me)</summary

Posted by eXamadeus on Jan 5, 2021, 4:18 PM

Tags

Related issues

Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments

reliability (Software)

296 - Permutation (with explanations)

eXamadeusJan 5, 2021, 4:18 PM

## Answer [Playground Link](https://www.typescriptlang.org/play?#code/C4TwDgpgBAChBOBbArsAhsAlgewHYB4AVAGigGkBeQgPigoCgomoBtQgXSggA9gJcAJgGdWuCADcE7RsyYB+VtNlMAXOS69+w8jOVQFLMqQB0puElQYcBAKLcAxgBtkAiEVJlq1JXqhqxkvD09KCQsAgofAJ04RboWHj4AORoSVAAPlBJAEZJtFAA9AWsKUmkOUmcmSwVGVmp7EA) (TypeScript 4.1+ only) ```ts type Permutation<T, K=T> = [T] extends [never] ? [] : K extends K ? [K, ...Permutation<Exclude<T, K>>] : never type Permuted = Permutation<'a' | 'b'> // ['a', 'b'] | ['b' | 'a'] ``` Whoa...that is weird looking. Don't worry I'll break it all down; weird piece, by weird piece 😆 <details> <summary>TLDR by @mistlog (Click me)</summary> > https://github.com/type-challenges/type-challenges/issues/614#issuecomment-790210311 </details> <details> <summary>Excellent Chinese translation by @zhaoyao91 (Click me)</summary> > https://github.com/type-challenges/type-challenges/issues/614#issuecomment-1312632631 </details> ## Explanation ### `[T] extends [never]` What in the bowels of 16-bit hell is that? Glad you asked. That my friends is a "feature" of TypeScript. It makes sense _eventually_, so just bear with me. Imagine you want to make a function called: `assertNever`. This function would be used for checking to make sure a type is...well...`never`. Pretty simple concept. You might want to use it to check some weird types you are building or something. Just roll with it, OK? <details> <summary>Wanna know a juicy secret? (Click me)</summary

Comments (0)

No comments yet. Start the discussion.

Comment on this issue