Cannot extend nitroApp interface
Environment
node v18.16.0, Nuxt 3.7.0 with Nitro 2.6.2
Reproduction
~/types/shim.d.ts
import { S3Client } from '@aws-sdk/client-s3';
declare module 'nitropack' {
interface NitroApp {
s3: S3Client;
}
}
export default {};
~/tsconfig.json
{
"extends": "./.nuxt/tsconfig.json",
"files": ["./types/shim.d.ts"]
}
~/server/tsconfig.json
{
"extends": "../.nuxt/tsconfig.server.json"
}
Describe the bug
When trying to access nitroApp.s3, I got:
Property 's3' does not exist on type 'NitroApp'.
Additional context
No response
Logs
No response
### Tasks
Hi, how are you attaching the client to the app? You are just augmenting the type definition in your description.
@Hebilicious Repro: https://stackblitz.com/edit/github-nbxfhb?file=server%2Fapi%2Ffoo.ts,server%2Findex.d.ts
- Download project
- Run
npx nuxi typecheck
For some reason it works on Stackblitz but doesn't work locally
The main thing is that with Nuxt, you need to install nitropack as a dependency to allow typescript augmenting it properly.
Nuxt could also provide a reexport /cc @danielroe wdyt?
you shouldn't need to install nitropack separately on recent versions of nuxt as we do some magic to hoist the types.
check that you're fully updated
I was having the same issue, but after checking defineNitroPlugin definition, I saw it uses:
import type { NitroAppPlugin } from "nitropack/types";
export declare function defineNitroPlugin(def: NitroAppPlugin): NitroAppPlugin;
So instead of using nitropack, I used nitropack/types. For @eesoymilk , it would become:
declare module 'nitropack/types' {
interface NitroApp {
s3: S3Client;
}
}
Also, I put the nitro.d.ts file inside of ./server/types/nitro.d.ts. No changes to tsconfig.json.
Both my IDE (VSCode) and pnpm nuxi typecheck were good and showing the correct type