H
Hono4mo ago
tcurdt

[solved] return type of request handler

Why is 1) is fine and 2) has a type problem? 1)
.get('/foo', async (c) => {
return await c.html(<body/>)
})
.get('/foo', async (c) => {
return await c.html(<body/>)
})
2)
.get('/bar', async (c) => {
return await render(c)
})

async function render(c) : Promise<HtlEscapedString> {
return await c.html(<body/>)
}
.get('/bar', async (c) => {
return await render(c)
})

async function render(c) : Promise<HtlEscapedString> {
return await c.html(<body/>)
}
I don't see it
2 Replies
Nico
Nico4mo ago
You don’t need to await the return of c. Also in future adding await before return is redundant. You should be able to call render without a return because once render makes a return to context it will continue to the next middleware or result
tcurdt
tcurdt4mo ago
Thanks, @Nico ... that wasn't the really the question though ... sorry, for the comment ... let me adjust this It seems like Promise<HtlEscapedString> is not a type that is valid to return yet it works in 1) but the tooling complains in 2) OK, I get it now
No overload matches this call.
The last overload gave the following error.
Argument of type '(c: Context<Env, "/bar", BlankInput>) => Promise<HtmlEscapedString>' is not assignable to parameter of type 'H<Env, "/bar", BlankInput, HandlerResponse<any>>'.
Type '(c: Context<Env, "/bar", BlankInput>) => Promise<HtmlEscapedString>' is not assignable to type 'MiddlewareHandler<Env, "/bar", BlankInput>'.
Type 'Promise<HtmlEscapedString>' is not assignable to type 'Promise<void | Response>'.
Type 'HtmlEscapedString' is not assignable to type 'void | Response'.
No overload matches this call.
The last overload gave the following error.
Argument of type '(c: Context<Env, "/bar", BlankInput>) => Promise<HtmlEscapedString>' is not assignable to parameter of type 'H<Env, "/bar", BlankInput, HandlerResponse<any>>'.
Type '(c: Context<Env, "/bar", BlankInput>) => Promise<HtmlEscapedString>' is not assignable to type 'MiddlewareHandler<Env, "/bar", BlankInput>'.
Type 'Promise<HtmlEscapedString>' is not assignable to type 'Promise<void | Response>'.
Type 'HtmlEscapedString' is not assignable to type 'void | Response'.
c.html() returns a response and hence the mismatch. So it's either
async function render(c) : Promise<HtlEscapedString> {
return (<body/>)
}

.get('/bar', async (c) => {
return c.html(render(c))
})
async function render(c) : Promise<HtlEscapedString> {
return (<body/>)
}

.get('/bar', async (c) => {
return c.html(render(c))
})
or
async function render(c) : Promise<Response> {
return c.html(<body/>)
}

.get('/bar', async (c) => {
return render(c)
})
async function render(c) : Promise<Response> {
return c.html(<body/>)
}

.get('/bar', async (c) => {
return render(c)
})
Want results from more Discord servers?
Add your server