Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TS] Defining the handler and the return type #4479

Open
jaclas opened this issue Feb 7, 2024 · 0 comments
Open

[TS] Defining the handler and the return type #4479

jaclas opened this issue Feb 7, 2024 · 0 comments
Labels
support Questions, discussions, and general support

Comments

@jaclas
Copy link

jaclas commented Feb 7, 2024

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:

export namespace Lifecycle {
    /**
     * 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.
     */
    type Method<
        Refs extends ReqRef = ReqRefDefaults,
        R extends ReturnValue<any> = ReturnValue<Refs>
    > = (
            this: MergeRefs<Refs>['Bind'],
            request: Request<Refs>,
            h: ResponseToolkit<Refs>,
            err?: Error | undefined
        ) => R;

So I declare the route object like this:

const poemsRoutings: ServerRoute<ReqRefDefaults, DataResultsObject>[] = [
    {
        method: "GET",
        path: "/api/poems/pages/count",
        options: {
            handler: poems.getPoemsPagesCountHandler,
            auth: {
                mode: "try"
            }
        }
    },

and the environment (vscode) correctly develops this type, showing that my type will be returned:

image

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?

@jaclas jaclas added the support Questions, discussions, and general support label Feb 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support Questions, discussions, and general support
Projects
None yet
Development

No branches or pull requests

1 participant