-
-
Notifications
You must be signed in to change notification settings - Fork 4.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string enum to union #30655
Comments
#30656 - Pull Request updated. |
Hi @lovetingyuan would you mind helping provide an answer here? |
yes, here is an answer: type EnumToUnion<T extends string> = `${T}` I just found the answer, but I don't really know how it works. |
Because in the test cases, you give the enum as a type to |
才发现这楼是不是都会说中文,, |
哦哦 确实 enum 和 typeof enum 是不一样的 |
I just wanted to say, I came to the same answer. This was a fun challenge. I do not usually use enums; nevertheless, this was an entertaining and informative challenge. |
I'm curious if anyone knows if there's a way to do something similar for number types as well? Say like you have a numerical enum, is there a way to turn that into a union of the number values? Is there something in TypeScript like type Demo = `${}`; but for |
enum Enum {
a,
b,
c,
}
type EnumValue = `${Enum}` extends `${infer N extends number}` ? N : never;
// ^? 0 | 1 | 2 Is this what you are looking for? |
Yes, works great! Thank you 🚀 |
Working on making the typings for declarating tag types a little simpler to setup/use So rather than `StringTag<Hey>` being just the plain enum value, it will accept either using the enum by it's properties, or by passing in the plain strings itself I like it in that you can declare things neatly with enums (which will later down the line also allow for mod-ability of your type definitions!), but still use the raw values themselves, without needing to import a reference to the enum by way of importing it So it's the ability to write type-safe, validated code, but without needing to statically import the types library itself, it's only for the IDE/hinting/type-checking step Say like you're writing a script that validates with Region-Types, you wouldn't have to import `EntityResource` from `region-types/java/entity`, it would just type-check the shape for you, by way of the type of the parent shape that you are accessing ```ts import type { Entity } from "region-types/java/entity"; declare const entity: Entity; entity.id = // you get autocomplete string values right here ``` Sorry my demo code names are so off the wall, it seems like I choose random funny ones 😂 ```ts () => { enum Hey { Nice = "nice", Hah = "hah" } type Hi = StringTag<Hey>; let hi: Hi; hi = "nice"; hi = Hey.Hah; enum Hag { Fart, Giggity, Poop, MooveOver } type Ooga = ByteTag<Hag>; let booga: Ooga; booga = new Int8(0); const v: ListTag<DoubleTag<Hag>> = []; v.push(0); } ``` Woah, this is actually a really cool unexpected concept! (Right here was a custom demo using my new `NumberLike` type, but to convert a `bigint` primitive type to a `number`-based one) You can use the same type inference to convert between `bigint` and `number` value types I wish there was a way to make `bigint`-based enums too, looks like it can only be with `string` or `number` values. I started making the demo for this implementation/concept a day or two ago, here was the file for that: ```ts // type-challenges/type-challenges#30655 // https://www.designcise.com/web/tutorial/how-to-create-a-union-type-from-enum-keys-in-typescript // https://stackoverflow.com/questions/68458593/how-to-create-an-enum-like-object-with-bigint-initializers-in-typescript // https://www.reddit.com/r/NoStupidQuestions/comments/1b7r28y/can_a_pizza_be_cut_into_one_slice/ class Double<T extends number = number> extends Number { constructor(value: T) { super(value); } override valueOf(): T { return super.valueOf() as T; } get [Symbol.toStringTag]() { return "Double" as const; } } type DoubleTag<T extends number = number> = Double<T>; type StringTag<T extends string = string> = T; enum Enum { a, b, c, } type EnumValue = `${Enum}` extends `${infer N extends number}` ? N : never; // ^? type NumberMaker<T extends number> = `${T}` extends `${infer N extends number}` ? N : never; type haha = `${Enum}`; // ^? type EnumUnion = NumberMaker<Enum>; // ^? interface Demo { Hi: DoubleTag<NumberMaker<Enum>>; Hey: StringTag<`${5}`>; } ```
Info
Transform a string enum to union type.
Question
Transform a string enum to union type.
Example:
Template
This is the template for challengers to start the coding. Basically, you just need to change the name of your generic/function and leave to implementation
any
.Test Cases
Provide some test cases for your challenge, you can use some utils from
@type-challenges/utils
for asserting.The text was updated successfully, but these errors were encountered: