You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[TS] Defining the handler and the return type #4479
I would like the handler to explicitly return the DataResultObject type (my object type), the handler type declaration in Hapi looks like this:
exportnamespaceLifecycle{/** * Lifecycle methods are the interface between the framework and the application. Many of the request lifecycle steps: * extensions, authentication, handlers, pre-handler methods, and failAction function values are lifecycle methods * provided by the developer and executed by the framework. * Each lifecycle method is a function with the signature await function(request, h, [err]) where: * * request - the request object. * * h - the response toolkit the handler must call to set a response and return control back to the framework. * * err - an error object available only when the method is used as a failAction value. */typeMethod<RefsextendsReqRef=ReqRefDefaults,RextendsReturnValue<any>=ReturnValue<Refs>>=(this: MergeRefs<Refs>['Bind'],request: Request<Refs>,h: ResponseToolkit<Refs>,err?: Error|undefined)=>R;
@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:
Activity
otajor commentedon Jun 4, 2024
@jaclas the
ServerRoutetype is different from the handler type. ServerRoute is the type for the entire config object, includingmethod,pathetc - 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.getPoemsPagesCountHandlerfunction. 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.damusix commentedon Jun 9, 2024
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
Lifecyclenamespace. BothLifecycle.MethodandServerRoutereceive the References generic, which you can use to augment the route. See the playground link below:@jaclas @RodolpheGohard @otajor
https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBACQIZmAGjgbzgZQKZQBuBAShAK4x4YAywAZngMYCeTANnnAL5z1QQQcAOQABABYpgAekmphAbgCwAKFUwWYLgAUIeECTz0AznAC8WVXDgBFcgRYAuSyuvWmSKgHNoLAPzOxjBQwAB2XlY8qtyqqkwQoUFwXngwuvrG2kgpxgDCFKEwyKEAJpxQznSMrBx4AHQAsqniECUAPOkGRsYAfOZwABRQeACOGOIAlOZ9mLGucPGJ8KH9AKzKavPDI3Uj9lAsdR7evuZmFsJIoSww4mFewnPWwzDkUCuh0RtxCUnx5IVOsZnPgiKQKFQOnouiY+hZZvMQM1Ws5hCkYMI0JEwJ5xKjpFJpJAMkTsnhjNJ-oVMZFJKVys50UCsjl8gCilcygQsfMIGAYMBfs4EW44EhKHiXKLrCBWnhUcEWI95tYYvM1Wqfks4MSDBDyf0ANqRKlpaHGVQAXQUQA