hoangvvo / next-connect

The TypeScript-ready, minimal router and middleware layer for Next.js, Micro, Vercel, or Node.js http/http2
https://www.npmjs.com/package/next-connect
MIT License
1.63k stars 65 forks source link

Typing incorrect? #129

Closed mikestopcontinues closed 3 years ago

mikestopcontinues commented 3 years ago

Hey, I'm new to typescript, but I'm pretty sure there's something going on with the types exported.

This expression is not callable.
Type 'NextConnect<NextApiRequest, NextApiResponse<any>>' has no call signatures.
ts(2349)

I wrap next-connect for use across a few related projects, and I want to add tests for the interface. So I'm manually calling the resulting api handler. A simplified case:

const getRouter = () => router(routeOpts);
const handler = getRouter().get((req, res) => res.send('ok'));

await handler({method: 'GET'}, mockRes); // TS error above

I believe the fix in index.d.ts is on lines ~26-28. Move the function declaration inside the interface:

  interface NextConnect<U, V> {
    (req: U, res: V): Promise<void>;

    use<T = {}, S = {}>(...handlers: Middleware<U & T, V & S>[]): this;
    // all the rest...

That said, I'm not totally sure. Like I said, I'm still learning the ropes with TS.

hoangvvo commented 3 years ago

You fix is correct and was applied thanks to #128. Can you try v0.10.1?

mikestopcontinues commented 3 years ago

Works perfectly. Thanks!