rsnext/packages
Björn f73e7d51ad
Improve NextApiHandler type for early returns (#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`
2022-04-16 04:00:52 +00:00
..
create-next-app v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
eslint-config-next v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
eslint-plugin-next v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next Improve NextApiHandler type for early returns (#35166) 2022-04-16 04:00:52 +00:00
next-bundle-analyzer v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-codemod v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-env v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-mdx v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-plugin-storybook v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-polyfill-module v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-polyfill-nomodule v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
next-swc v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
react-dev-overlay v12.1.6-canary.2 2022-04-15 14:08:42 -05:00
react-refresh-utils v12.1.6-canary.2 2022-04-15 14:08:42 -05:00