#Shipping TS enums in JS library

11 messages · Page 1 of 1 (latest)

forest jungle

How does DJS do this? I'm currently just using Object.freeze({ key: value }) and the types in my typescript project that uses this js library get pretty messy

toxic tinselBOT
  • Consider reading #how-to-get-help to improve your question!
  • Explain what exactly your issue is.
  • Post the full error stack trace, not just the top part!
  • Show your code!
  • Issue solved? Press the button!
forest jungle

every time i want to reference an enum i have to use (typeof Enum)[keyof typeof Enum] with my current method which sucks

and btw i only use ts to generate types, can i modify that in some way to include enums

solemn panther

from what I can find on d.js monorepo, they seem to use the function in its dedicated file called createEnum in src/util/Enums.js
2 places I can find seems to call that function in Partials.js and Status.js

forest jungle

i know that but im kinda confused as to how they tell that any objects created using this function are enums

ok so the Enums.js is just created to map keys to numbers

for example if they put ready as the first index then Ready's value in the enum will be 0

but that's not what i'm trying to do at all

stone umbra
forest jungle

hmm so can i create a typescript enum in js? can i just copy the compiled code for enums and use that to recreate them in a plain js lib?