N
Nuxt6d ago
Cesxhin

Ignore "type" for variables enum after build of module

I created simple enum:
file: module.ts

export enum test {
HELLO = "hello"
}
file: module.ts

export enum test {
HELLO = "hello"
}
After build into file types.d.ts there is "type test" but not work on project because I get this error from typescript: "'test' cannot be used as a value because it was exported using 'export type'.ts-plugin(1362)"
6 Replies
kapa.ai
kapa.ai6d ago
Hi @Cesxhin. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- Based on the information provided in the knowledge sources, there isn't enough specific information to directly address your issue with the enum type after building a module. However, I can provide some general guidance related to TypeScript and Nuxt module development that might be helpful. When developing Nuxt modules, it's important to ensure proper type declarations. The issue you're experiencing seems to be related to how TypeScript is handling the enum declaration during the build process. To potentially resolve this issue, you could try the following: 1. Make sure you're using the latest version of @nuxt/module-builder to build your module. You can use the nuxi build-module command as mentioned in the Nuxt documentation. 2. Check your tsconfig.json file to ensure it's correctly configured for module development. The Nuxt-generated TypeScript configuration should handle most cases automatically, as noted in the Stack Overflow answer. 3. If the issue persists, you might need to explicitly declare your enum without using the type keyword. Instead of export enum, try using export const enum or just export enum without type. 4. If you're still encountering issues, you may need to add a type declaration file (.d.ts) manually to ensure the enum is correctly typed after the build process. If these suggestions don't resolve the issue, you might want to check for any recent updates or known issues with TypeScript and Nuxt module development. The Nuxt community is actively working on improving TypeScript support, as evidenced by recent pull requests like this one which aims to improve type declarations for virtual files. Remember that when developing Nuxt modules, it's crucial to test thoroughly with different TypeScript versions and Nuxt configurations to ensure compatibility. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6d ago
<@552606377067937792> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Cesxhin
CesxhinOP6d ago
@kapa.ai I already decleare export const enum
kapa.ai
kapa.ai6d ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I apologize, but I don't have enough specific information in the provided knowledge sources to address your issue with export const enum in the context of Nuxt module development. The documents don't contain detailed information about enum declarations or the specific error you're encountering. However, since you've already tried using export const enum, which is generally a good practice, the issue might be related to how the module is being built or how TypeScript is configured in your Nuxt project. To troubleshoot this further, you could try the following: 1. Check your tsconfig.json file to ensure it's correctly configured. As mentioned in the Nuxt TypeScript documentation, make sure your tsconfig.json extends from the Nuxt-generated one:
{
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
// Your custom options here
}
}

{
"extends": "./.nuxt/tsconfig.json",
"compilerOptions": {
// Your custom options here
}
}

2. If you're developing a Nuxt module, ensure you're using the latest version of @nuxt/module-builder and building your module with the nuxi build-module command. 3. If the issue persists, it might be worth checking for any TypeScript-related issues in the Nuxt GitHub repository or considering reporting this as a potential bug. If you need more specific help, you might want to create a minimal reproduction of the issue and seek help through the Nuxt community channels or by reporting a bug if you believe it's a Nuxt-specific issue. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai6d ago
<@552606377067937792> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Reinier Kaper
Reinier Kaper6d ago
I wouldn't use TS enums, just use constants exported as types, like this:
export const TEST = {
Hello: 'hello',
} as const
Object.freeze(TEST)
export type TEST = (typeof TEST)[keyof typeof TEST]
export const TEST = {
Hello: 'hello',
} as const
Object.freeze(TEST)
export type TEST = (typeof TEST)[keyof typeof TEST]
That way you can just use import { TEST } from '~/constants and use it as a type as well (you can also import type { TEST } from '~/constants if you only need the type)

Did you find this page helpful?