nitro icon indicating copy to clipboard operation
nitro copied to clipboard

Cannot extend nitroApp interface

Open eesoymilk opened this issue 2 years ago • 2 comments

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

eesoymilk avatar Sep 02 '23 18:09 eesoymilk

Hi, how are you attaching the client to the app? You are just augmenting the type definition in your description.

Hebilicious avatar Sep 04 '23 12:09 Hebilicious

@Hebilicious Repro: https://stackblitz.com/edit/github-nbxfhb?file=server%2Fapi%2Ffoo.ts,server%2Findex.d.ts

  1. Download project
  2. Run npx nuxi typecheck

For some reason it works on Stackblitz but doesn't work locally

enkot avatar Mar 11 '24 10:03 enkot

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?

pi0 avatar Nov 08 '24 11:11 pi0

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

danielroe avatar Nov 08 '24 11:11 danielroe

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

varugasu avatar Nov 20 '24 20:11 varugasu