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
- [BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
- Feature Request: Auto-resume CLI session when usage limit resets
- Please deobfuscate the Python
- [Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
- # 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
- [FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Raise real-world problems, find people facing the same issue, and discuss solutions.3,000 issues0 comments
reliability (Software)
[BUG] Cowork (Windows): all Plan9 shares fail with "Plan9 mount failed: invalid argument" after Windows update KB5124008 (26200.9445) — uninstalling the KB fixe
Feature Request: Auto-resume CLI session when usage limit resets
Please deobfuscate the Python
[Bug] MLX version of Gemma 4 26B fails to load: "Model type gemma4 not supported" (macOS)
# 233 Developers Discussed Abandoning the Office Add-in Platform — An Open Letter on the Office.js Stability, Security & Trust Crisis
[FEATURE] Allow users to configure which shell the Bash tool uses or inherit initial shell
Admin Materials and Surfaces
AMD Support
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
Comments (0)
No comments yet. Start the discussion.