Skip to content

Commit

Permalink
feat(question): add #30655 - string enum to union
Browse files Browse the repository at this point in the history
  • Loading branch information
lovetingyuan committed Nov 3, 2023
1 parent 49ff55e commit 0399690
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions questions/30655-medium-string-enum-to-union/README.md
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"
```
7 changes: 7 additions & 0 deletions questions/30655-medium-string-enum-to-union/info.yml
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

3 changes: 3 additions & 0 deletions questions/30655-medium-string-enum-to-union/template.ts
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.
23 changes: 23 additions & 0 deletions questions/30655-medium-string-enum-to-union/test-cases.ts
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>,
]

0 comments on commit 0399690

Please sign in to comment.