NotFoundHandler TypeScript Error
I am getting a typescript error that I just don't know how to resolve and looking for some help to why I am getting the error.
I have created a custom notFound handler function and I am adding it to my app. ts file
notFound.ts
app.ts
import {OpenAPIHono} from '@hono/zod-openapi'
import {notFound} from './middlewares/notFound.ts'
import {load} from '@std/dotenv'
const app = new OpenAPIHono()
await load({export: true})
const portNumber = Number(Deno.env.get('PORT'))
app.get('/', (c) => {
return c.text('Hello Hono!')
})
app.notFound(notFound)
The error is coming from the line: app.notFound(notFound)
Here is the Type Error
Argument of type 'NotFoundHandler' is not assignable to parameter of type 'NotFoundHandler<Env>'.
Types of parameters 'c' and 'c' are incompatible.
Type 'Context<Env, any, {}>' is missing the following properties from type 'Context<any, any, {}>':
#rawRequest,
#req,
#var,
#status
and 10 more.deno-ts(2345)
⚠ Error (TS2345) |
Argument of type
is not assignable to parameter of type .
Types of parameters c and c are incompatible. Type is missing the following properties from type : #rawRequest #req #var #status and 10 more. (alias) const notFound: NotFoundHandler import notFound Can shed some light on this for me?
Types of parameters c and c are incompatible. Type is missing the following properties from type : #rawRequest #req #var #status and 10 more. (alias) const notFound: NotFoundHandler import notFound Can shed some light on this for me?
4 Replies
Hi, if you type the function, you don't need to type the function parameters.
Anyway it's giving you error because NotFoundHandler to the C parameter assign a different type than the one you gave.
NotFoundHandler for c parameter extends Context.
I am not sure how to fix this I am using hono/zod-openapi if I create the hono instance with app = new Hono() i do not get this type error. If I use new OpenAPIHono() I get the type error. I am not a typescript wizard. So my thinking is that I have somehow tell typescript that the types from hono should apply to the tpyes from hono/zod-open api. Does this mean I should extend the NotFoundHandler type?
@pietrodev
This works only if I set "noImplicitAny": false in deno.json. app.notFound is now comming from hono/zod-openapi and not from hono am I thinking of that correctly