#Replicate exported enum from library when only importing type

1 messages · Page 1 of 1 (latest)

foggy gale
#

So, this is weird, and there's probably a better way to do this, but is there a way to type an enum as another enum?

Basically, I have a large client library (discord.js) with some enums that I'm referencing in my documentation package which is imported into both my client app and my web app. I want to use those enums from discord.js in the documentation library, but I don't want to have to import that whole client-side node package into my web app to do so.

My first thought is to redeclare the enums myself, and type them from an import type with the source enum, but I can't seem to figure out how to make that work. However, I can't figure out syntax for it. This sort of thing doesn't work:

import type { ApplicationCommandOptionType as ApplicationCommandOptionTypeSource } from 'discord.js';
enum ApplicationCommandOptionType {
    Subcommand = 1,
    SubcommandGroup = 2,
    String = 3,
    Integer = 4,
    Boolean = 5,
    User = 6,
    Channel = 7,
    Role = 8,
    Mentionable = 9,
    Number = 10,
    Attachment = 11
}
const test = ApplicationCommandOptionType as ApplicationCommandOptionTypeSource; //Conversion of type 'typeof ApplicationCommandOptionType' to type 'ApplicationCommandOptionType' may be a mistake because neither type sufficiently overlaps with the other.

Is there a better way to accomplish this? I want to maintain the type safety with these enums while only importing the types from the discord.js library.

austere gust
#

maybe you want ApplicationCommandOptionType satisfies typeof ApplicationCommandOptionTypeSource?

#

you could also just use the ApplicationCommandOptionType type from discord.js at usage sites and rely on type errors appearing there if you screw anything up. you could either not have anything here and just use number literals at usage sites, or only export a runtime enum-like object here but not the type

#

happy to elaborate on any of that if it doesn't make sense. feel free to ask follow-up questions

foggy gale
#

though I had to end up with a const object instead of an enum. But mostly same practical usage

austere gust
cloud tideBOT
#
mkantor#0

Preview:```ts
import type {ApplicationCommandType as ApplicationCommandTypeSource} from "discord.js"

export const ApplicationCommandType: typeof ApplicationCommandTypeSource =
{
ChatInput: 1,
User: 2,
Message: 3,
PrimaryEntryPoint: 4,
}```

foggy gale
austere gust
#

that kind of thing happens to me all the time 😅