f73e7d51ad
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` |
||
---|---|---|
.. | ||
create-next-app | ||
eslint-config-next | ||
eslint-plugin-next | ||
next | ||
next-bundle-analyzer | ||
next-codemod | ||
next-env | ||
next-mdx | ||
next-plugin-storybook | ||
next-polyfill-module | ||
next-polyfill-nomodule | ||
next-swc | ||
react-dev-overlay | ||
react-refresh-utils |