Non-primitives as enum values #826
-
Hey 👋! I've got a rather simple question: is it safe to use non-primitives as the values of an enum? In my particular case these would be mongoose models (=objects). I have tried it before asking and it seems to work just fine, so I'm kind of looking for confirmation. Unfortunately I haven't found anything regarding this in the docs, the closest I have is this from the graphql-js docs:
Some sample code, in case it's helpful: export const MyEnum = {
Foo: FooModel,
Bar: BarModel
};
export type MyEnum = typeof MyEnum[keyof typeof MyEnum];
registerEnumType(MyEnum, { name: "MyEnum" }); Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
TypeScript support only string and number enum members, so you can't use arbitrary things as enum values. If you cheat and use JS objects as fake enums in |
Beta Was this translation helpful? Give feedback.
TypeScript support only string and number enum members, so you can't use arbitrary things as enum values.
If you cheat and use JS objects as fake enums in
registerEnumType
, be aware that now you're on your own and things can break, especially between updates in the future.