Skip to content

TypeScript throws an error due to defineHandler attempting to use unexported types #4344

Description

@KTrain5169

Environment

Nitro v3.0.260610-beta
Vite v8.0.16
Node.js v24.16.0

Reproduction

Simply copy and pasting this code into a route (e.g. routes/api/index.ts):

import { defineHandler } from "nitro";

export default defineHandler((e) => {
    return { api: 'works!' }
})

should throw an error about EventHandlerRequest not being named without a reference to h3 types (full error below)

My tsconfig:

// tsconfig.server.json
{
  "extends": "./tsconfig.base.json",
  "compilerOptions": {
    "types": ["node", "./worker-configuration.d.ts"],
  },
  "include": ["routes", "api", "tasks", "plugins", "db"]
}
// tsconfig.base.json
{
  // Visit https://aka.ms/tsconfig to read more about this file
  "compilerOptions": {
    // File Layout
    // "rootDir": "./src",
    // "outDir": "./dist",

    // Environment Settings
    // See also https://aka.ms/tsconfig/module
    "module": "preserve",
    "moduleResolution": "bundler",
    "target": "es2025",
    "types": [],
    // For nodejs:
    // "lib": ["esnext"],
    // "types": ["node"],
    // and npm install -D @types/node

    // Other Outputs
    "sourceMap": true,
    "declaration": true,
    "declarationMap": true,
    "composite": true,

    // Stricter Typechecking Options
    "noUncheckedIndexedAccess": true,
    "exactOptionalPropertyTypes": true,

    // Style Options
    // "noImplicitReturns": true,
    // "noImplicitOverride": true,
    // "noUnusedLocals": true,
    // "noUnusedParameters": true,
    // "noFallthroughCasesInSwitch": true,
    // "noPropertyAccessFromIndexSignature": true,

    // Recommended Options
    "strict": true,
    "jsx": "react-jsx",
    "jsxImportSource": "preact",
    "paths": {
      "react": ["./node_modules/preact/compat"],
      "react-dom": ["./node_modules/preact/compat"],
    },
    "verbatimModuleSyntax": true,
    "noUncheckedSideEffectImports": true,
    "moduleDetection": "force",
    "skipLibCheck": true,
  }
}

Describe the bug

When using the define helper for a handler, TypeScript throws the error below.

I would help fix the issue, but I'm not sure if the problem lies within h3 or Nitro.

Additional context

No response

Logs

The inferred type of 'default' cannot be named without a reference to 'EventHandlerRequest' from '.pnpm/h3@2.0.1-rc.22_crossws@0.4.6_srvx@0.11.16_/node_modules/h3'. This is likely not portable. A type annotation is necessary.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions