-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(question): add #30655 - string enum to union
- Loading branch information
1 parent
49ff55e
commit 0399690
Showing
4 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
Transform a string enum to union type. | ||
|
||
Example: | ||
```ts | ||
enum A { | ||
a = 'aa', | ||
b = 'bb', | ||
c = 'cc' | ||
} | ||
|
||
type Result = EnumToUnion<A>; // expected to be "aa" | "bb" | "cc" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
difficulty: medium | ||
title: string enum to union | ||
tags: union, enum, string | ||
author: | ||
github: lovetingyuan | ||
name: tingyuan | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
enum YourEnum {} | ||
type EnumToUnion = any; // write your anwser here. | ||
type UnionType = EnumToUnion<YourEnum>; // should be a string union. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { Equal, Expect } from '@type-challenges/utils' | ||
import { ExpectFalse, NotEqual } from '@type-challenges/utils' | ||
|
||
enum A { | ||
a = 'aa', | ||
b = 'bb', | ||
c = 'cc' | ||
} | ||
|
||
enum B { | ||
a = 'aa', | ||
} | ||
|
||
enum C { | ||
a, b | ||
} | ||
|
||
type cases = [ | ||
Expect<Equal<EnumToUnion<A>, 'aa' | 'bb' | 'cc'>>, | ||
Expect<Equal<EnumToUnion<B>, 'aa'>>, | ||
// @ts-expect-error | ||
EnumToUnion<C>, | ||
] |