Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve NextApiHandler type for early returns (vercel#35166)
I would like to be able to write handlers with early returns like this: ```typescript import { NextApiHandler } from 'next' const handler: NextApiHandler = (req, res) => { const value = getStuff() if (value === 'branch') { return res.json({}) } res.status(400) } ``` but `NextApiHandler`'s current return type is `void | Promise<void>`, which causes compilation to fail with ``` Error:(11, 3) TS2322: Type '(req: NextApiRequest, res: NextApiResponse<any>) => Promise<NextApiResponse<any> | undefined>' is not assignable to type 'NextApiHandler<any>'. Type 'Promise<NextApiResponse<any> | undefined>' is not assignable to type 'void | Promise<void>'. Type 'Promise<NextApiResponse<any> | undefined>' is not assignable to type 'Promise<void>'. Type 'NextApiResponse<any> | undefined' is not assignable to type 'void'. Type 'NextApiResponse<any>' is not assignable to type 'void'. ``` to avoid that the above snippet needs to be written as ```typescript if (value === 'branch') { res.json({}) return } ``` which looks odd to me. Changing the return type of `NextApiHandler` to `unknown | Promise<unknown>` would allow for shorter early returns and still communicates to users that nothing is expected to be returned from a handler. Augmenting the type like this, makes the first snippet work: ```typescript import { NextApiRequest, NextApiResponse } from 'next' declare module 'next' { export declare type NextApiHandler<T = any> = ( req: NextApiRequest, res: NextApiResponse<T> ) => unknown | Promise<unknown> } ``` ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `yarn lint`
- Loading branch information