#types.d.ts
20 messages · Page 1 of 1 (latest)
Create a folder called types
add whatever interfaces you like
like User.d.ts
declare interface User {
email: string
id: string
username: string
}
Nuxt 3 will automatically make them available to your entire project - for all components/pages/composables/stores/etc.
Thank you, I am getting this error.
The problem was to name the file components.d.ts, I changed it to components.ts
what does that file (components.ts) look like?
it has to use that syntax of declare interface if you want to use .d.ts file naming
Is there a need to declare or export tbh?
I mean this just works and gets available all over the project:
// types/filename.ts
interface Hamster {
fluffiness: number;
}
But i'm a typescript noob so I might be missing something
I'm wondering, why is the "types" folder not mentioned in the Nuxt Guide? Is it a Nuxt built-in directory or a Node one?
In theory, it doesn't have to be types even
If you look at .nuxt/tsconfig.json, you'll find:
"include": [
"./nuxt.d.ts",
"../**/*"
],
yeah, doesn't have to be "types" - just a convention
some people put their interfaces/types right next to related components, or just in a single types.ts file
Thanks for posting this request for guidance on types / interfaces. I tried to follow the suggestion above, but it does not appear Nuxt3 is picking up the interfaces and importing automatically. I'm using VSCode, Nuxt 3.0.0. I have a the following structure:
/types/User.d.ts
declare interface IsUser {
_id: string;
fullName: string;
userType: UserTypeEnum;
}
declare interface IsUserSetting {
dark: boolean;
defaultOrg: string | null;
reportsShowList: boolean;
}
/components/User/Dialog.vue
<script setup lang="ts">
...
let user = ref<IsUser>({ ...BLANK_USER });
...
</script>
...
VSCode is complaining that "IsUser" is not found. Can you have multiple interface declarations per file? Could this be an ESLint config error? Thanks for your help.
Do you have Volar installed? https://marketplace.visualstudio.com/items?itemName=Vue.volar