N
Nuxt3mo ago
MrBear038

How do I write types for an alias?

Hey fellow Nuxters, I've been looking for ways of writing types for an alias to a file which contains a default export. I have not been able to come across a working solution as my editor keeps underlining the import saying it doesn't have a corresponding type declaration. This is the code for the type I'm writing
import resource from 'random-package/assets.json';

export type Resources = keyof typeof resource;

declare module '#alias' {
export default Array<keyof typeof resource>;
}
import resource from 'random-package/assets.json';

export type Resources = keyof typeof resource;

declare module '#alias' {
export default Array<keyof typeof resource>;
}
Any suggestions would be appreciated 🤧😅
11 Replies
kapa.ai
kapa.ai3mo ago
Hi @MrBear038. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- To write types for an alias in Nuxt, you should ensure that your alias is properly configured in your nuxt.config.ts under the alias property. Nuxt will automatically add these aliases to the generated TypeScript configuration files, which helps with type support and auto-complete in your IDE. However, if your editor complains about missing type declarations for an alias (like #alias), you need to provide a type declaration file for that alias. Here's how you can approach this: 1. Configure the Alias in nuxt.config.ts:
export default defineNuxtConfig({
alias: {
'#alias': '/absolute/path/to/your/file'
}
})
export default defineNuxtConfig({
alias: {
'#alias': '/absolute/path/to/your/file'
}
})
This ensures Nuxt and TypeScript are aware of the alias and will include it in the generated .nuxt/tsconfig.*.json files, providing path and type support automatically Nuxt Configuration - alias. 2. Add a Type Declaration for the Alias: Create a .d.ts file (e.g., types/alias.d.ts) in your project and declare the module for your alias:
// types/alias.d.ts
declare module '#alias' {
const resource: Array<string>; // Adjust this to match your actual export
export default resource;
}
// types/alias.d.ts
declare module '#alias' {
const resource: Array<string>; // Adjust this to match your actual export
export default resource;
}
If your file exports a default value, make sure to use export default in your type declaration. 3. Ensure TypeScript Picks Up the Declaration: Make sure your tsconfig.json includes the types directory or the specific .d.ts file, or simply let Nuxt's auto-generated configs handle it (recommended). If you need to customize, use the alias property in nuxt.config.ts rather than overriding compilerOptions.paths directly, as this can break Nuxt's auto-generated module resolutions TypeScript & Aliases. Note: If you want to provide more specific types (like keyof typeof resource), you can import the JSON in your type declaration and use typeof as you did:
import resource from 'random-package/assets.json';

declare module '#alias' {
const value: Array<keyof typeof resource>;
export default value;
}
import resource from 'random-package/assets.json';

declare module '#alias' {
const value: Array<keyof typeof resource>;
export default value;
}
Summary:
- Use the alias property in nuxt.config.ts for path aliases. - Provide a .d.ts file declaring the module and its types for the alias. - Let Nuxt handle the rest via its auto-generated TypeScript configs. If your editor still doesn't recognize the types, ensure you've run nuxt prepare, nuxt dev, or nuxt build so that all type files are generated and up to date Auto-generated Types. If you need to add or augment types from a Nuxt module, you can use the addTypeTemplate utility in a custom module, which will add a type declaration and reference it in the generated nuxt.d.ts file Adding Type Declarations. If you need more granular control, you can use the prepare:types hook to register a callback that will inject your types. If you follow these steps and still encounter issues, please provide more details about your alias and the file it points to, so we can help further. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@789952274804899881> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
MrBear038
MrBear038OP3mo ago
@kapa.ai it's not working
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- I'm sorry to hear it's not working. Based on the knowledge sources, here are the key points to ensure type declarations for an alias work in Nuxt: 1. Alias Configuration
Make sure your alias is defined in the alias property of your nuxt.config.ts. Nuxt will automatically add these aliases to the generated TypeScript configuration files, which should provide type support and path auto-complete for your aliases like #alias Nuxt Configuration - alias. 2. Type Declaration File
If you want TypeScript to recognize types for an alias, you need to create a .d.ts file that declares the module for your alias. For example:
// types/alias.d.ts
declare module '#alias' {
const value: Array<string>; // Adjust to match your export
export default value;
}

// types/alias.d.ts
declare module '#alias' {
const value: Array<string>; // Adjust to match your export
export default value;
}

If you want to use keyof typeof resource, you need to ensure that the type declaration can access the type of the resource. However, the knowledge sources do not provide a working example of importing a JSON file and using its type in a module declaration for an alias. 3. TypeScript Configuration
The knowledge sources recommend not to override compilerOptions.paths in your tsconfig.json, as this can break Nuxt's auto-generated module resolutions. Instead, rely on the alias property in nuxt.config.ts Directory Structure - tsconfig. 4. Type Inclusion
You do not need to manually include your types directory in tsconfig.json—Nuxt includes all .d.ts files by default. If you declare types in the global namespace, they will be available everywhere without explicit imports Stack Overflow: Type Definitions in Nuxt. Limitations:
The knowledge sources do not provide a direct, working example of declaring types for an alias that points to a JSON file and using keyof typeof resource in the module declaration. If your editor still underlines the import, it may be due to limitations in how TypeScript resolves types for JSON imports or how the alias is set up. Summary:
- Use the alias property in nuxt.config.ts. - Create a .d.ts file declaring the module for your alias. - Do not override compilerOptions.paths in tsconfig.json. - Let Nuxt handle type inclusion. If you have followed these steps and it still doesn't work, the knowledge sources do not contain enough information to provide a guaranteed solution for your specific use case. You may need to provide more details or check for issues with your editor or TypeScript setup. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@789952274804899881> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
MrBear038
MrBear038OP3mo ago
@kapa.ai I did some more debugging and found out that when adding an alias to the nuxt.config through the module, the path to the file giving the ts 2307 error doesn't include the extension. So how can I go about solve this problem? I'm assigning the value of the alias to be the dst property of .addTemplate()
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- When you add an alias in your Nuxt module and point it to the dst property of a file generated by addTemplate(), TypeScript may throw a TS2307 error ("Cannot find module") if the import path does not include the file extension. This is because TypeScript's module resolution is stricter than Node's and often requires the correct extension for non-JS/TS files, especially for virtual files generated by Nuxt. From the knowledge sources, there is a relevant issue and PR that addresses this exact scenario: - If you assign an alias to the dst of a template generated by addTemplate(), you should not include the file extension in the import path when importing it elsewhere. Including the extension can cause resolution errors, as TypeScript and Vite may not resolve the file correctly. Removing the extension from the import path fixes the error:
If there's a file extension for a middleware in your module you get this error in the downstream project... Removing the extension fixes the error.
docs: update addRouteMiddleware path
What to do: - When you import the file via the alias, omit the file extension.
For example, if your template is my-template.mjs and your alias is #my-template, import it as:
import myTemplate from '#my-template'

import myTemplate from '#my-template'

not
import myTemplate from '#my-template.mjs'

import myTemplate from '#my-template.mjs'

- Ensure your alias in nuxt.config.ts or your module points to the correct dst path, but when consuming the alias, always omit the extension. If you follow this pattern and still encounter issues, double-check that your alias and template paths are correct and that the template is being generated as expected. If you need to support both JavaScript and TypeScript consumers, always document that the extension should be omitted in imports. Summary:
Omit the file extension when importing a template via an alias that points to a file generated by addTemplate(). This resolves the TS2307 error according to the Nuxt documentation and recent PRs docs: update addRouteMiddleware path. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@789952274804899881> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
MrBear038
MrBear038OP3mo ago
@kapa.ai The extension is being omitted when I inspect the tsconfg.json file for the alias definition. When I'm importing the alias in my project I don't include the extension in the import.
kapa.ai
kapa.ai3mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Thank you for clarifying that the extension is omitted both in your alias definition and in your import statements. Based on the knowledge sources, here’s what is relevant: - Nuxt’s addTemplate() generates a file at the dst path, and you can assign an alias to this path in your nuxt.config.ts or module. When importing, you should omit the extension, which you are already doing. - Nuxt automatically adds aliases defined in nuxt.config.ts to the generated TypeScript configuration files, so TypeScript should recognize them for type checking and auto-complete Nuxt Configuration - alias. - If you override compilerOptions.paths in your own tsconfig.json, it can break Nuxt’s auto-generated module resolutions, so you should avoid this and rely on the alias property in nuxt.config.ts Directory Structure - tsconfig. - There is no explicit mention in the knowledge sources of a required workaround if the alias points to a file generated by addTemplate() and the extension is omitted, but TypeScript still cannot resolve the module. If you are still getting TS2307 errors despite following these practices, the knowledge sources do not provide a direct solution or further troubleshooting steps for this specific scenario. There may be an edge case or a bug, but based on the official documentation and related issues, your approach should work if everything is set up as described. If the problem persists, you may want to: - Double-check that the template file is actually being generated at the expected location. - Ensure that the Nuxt build process has run (nuxi prepare, nuxt dev, or nuxt build) so that all type files and templates are up to date Auto-generated Types. - Make sure you are not overriding compilerOptions.paths in your root tsconfig.json. If after these checks the issue remains, the knowledge sources do not contain enough information to provide a definitive answer for your case. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai3mo ago
<@789952274804899881> Kapa.ai is still learning and improving, please let me know how I did by reacting below.

Did you find this page helpful?