I have something like this:
// enums.ts
export enum UserAction {
Login,
Logout
}
export enum PostAction {
DeletePost,
CreatePost
}
And in another file, I want to create a union type from all the exported items in "enums.ts"
I tryed to write code like below, but something went wrong. It works only when I join them hand by hand.
import * as types from "./enums";
import { PostAction, UserAction } from "./enums";
type MyOldUnion = PostAction | UserAction; // works
type MyUnion = typeof types[keyof typeof types]; // not working
function useUnionType(type: MyUnion) {}
function useOldUnionType(type: MyOldUnion) {}
// TS2345: Argument of type 'PostAction.CreatePost' is not assignable to parameter of type 'MyUnion'.
useUnionType(PostAction.CreatePost);
useOldUnionType(PostAction.CreatePost);
Is there a way to create a union type programmatically from a namespace?
Or maybe it's a bad choice to do so in typescript?
My typescript version: 3.9.7