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
export const notFound: NotFoundHandler = (c: Context) => {
return c.json({message: `Not Found - ${c.res.url}`}, 404)
}
export const notFound: NotFoundHandler = (c: Context) => {
return c.json({message: `Not Found - ${c.res.url}`}, 404)
}
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)
import {Hono} from 'hono'
import {notFound} from './middlewares/notFound.ts'
import {load} from '@std/dotenv'
const app = new Hono()

await load({export: true})
const portNumber = Number(Deno.env.get('PORT'))

app.get('/', (c) => {
return c.text('Hello Hono!')
})

app.notFound(notFound)
Deno.serve({port: portNumber}, app.fetch)
import {Hono} from 'hono'
import {notFound} from './middlewares/notFound.ts'
import {load} from '@std/dotenv'
const app = new Hono()

await load({export: true})
const portNumber = Number(Deno.env.get('PORT'))

app.get('/', (c) => {
return c.text('Hello Hono!')
})

app.notFound(notFound)
Deno.serve({port: portNumber}, app.fetch)
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?
No description
No description
No description
4 Replies
pietrodev07
pietrodev073w ago
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.
jamesfoley
jamesfoleyOP3w ago
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
pietrodev07
pietrodev073w ago
export const notFound: NotFoundHandler = (c) => {
// rest of your code
}
export const notFound: NotFoundHandler = (c) => {
// rest of your code
}
jamesfoley
jamesfoleyOP3w ago
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
Want results from more Discord servers?
Add your server