Open jaclas opened 7 months ago
@jaclas the ServerRoute
type is different from the handler type. ServerRoute is the type for the entire config object, including method
, path
etc - it takes 0 or 1 type arguments. handler is the type for the handler function only, which can take 2 type arguments.
To type the handler properly I think you just need to put the correct return type on your poems.getPoemsPagesCountHandler
function. It looks like Typescript is already correctly picking up the return type, which is unrelated to you passing it to ServerRoute, so maybe you're doing this already.
If you want to separate your handlers out to some sort of controller abstract, you need to type them accordingly. For this, you can use the Lifecycle
namespace. Both Lifecycle.Method
and ServerRoute
receive the References generic, which you can use to augment the route. See the playground link below:
@jaclas @RodolpheGohard @otajor
Runtime
Node
Runtime version
18
Module version
21.3.3
Used with
standalone
Any other relevant information
No response
How can we help?
I would like the handler to explicitly return the DataResultObject type (my object type), the handler type declaration in Hapi looks like this:
So I declare the route object like this:
and the environment (vscode) correctly develops this type, showing that my type will be returned:
But at the same time, I get a TS compilation error (which you can also see in the screenshot) that reads:
Generic type 'ServerRoute<Refs>' requires between 0 and 1 type arguments.ts(2707)
How to do this correctly?