rsnext/packages
mattbrandlysonos 305214476b
fix NextApiRequestCookies and NextApiRequestQuery types (#25532)
Hello! Thanks for making next.js so great.

## Bug

Right now, these types give false confidence. These `key`s are treated as though [a value is defined for _every_ string](https://dev.to/sarioglu/avoiding-unintended-undefined-values-while-using-typescript-record-4igo). However, given an arbitrary request, a particular cookie or query param could be `undefined`.

For example, when building an `/api` endpoint, the code might look like this:

```ts
import type { NextApiRequest, NextApiResponse } from "next"

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  // According to the old types, `value` is a string
  const value = req.cookies.value

  // Type-checking passes but leads to a runtime error when no `value` cookie is provided in the request
  //   Uncaught TypeError: Cannot read property 'toLowerCase' of undefined
  value.toLowerCause()

  // ...
}
```

By using `Partial`, TypeScript now knows that these objects don't have values defined for every `key` and accessing a given `key` might resolve to `undefined`.

---

The only obvious error this caused within this repo was on line 333 of the same file. For better or worse, I ended up casting that cookie value to a `string`. There's a series of `if` statements before it that, I guess, are guaranteeing that it's truly a string. Potentially, that stretch could be refactored such that TypeScript _knows_ it's a string.

Also, I tried to follow the contributing guidelines. However, running `yarn types` kicked out a bunch of errors about overwriting files:

```
$ yarn types
yarn run v1.22.10
$ lerna run types --stream
lerna notice cli v4.0.0
lerna info Executing command in 2 packages: "yarn run types"
@next/env: $ tsc index.ts --declaration --emitDeclarationOnly --declarationDir types --esModuleInterop
next: $ tsc --declaration --emitDeclarationOnly --declarationDir dist
next: error TS5055: Cannot write file '/Users/mbrandly/code/next.js/packages/next/dist/build/index.d.ts' because it would overwrite input file.
next: error TS5055: Cannot write file '/Users/mbrandly/code/next.js/packages/next/dist/build/webpack/plugins/build-manifest-plugin.d.ts' because it would overwrite input file.
...
...
...
```

Let me know if there's anything I can improve here! Thanks again.
2022-05-23 00:48:26 +00:00
..
create-next-app Merge multiple log statements (#35310) 2022-05-22 13:50:08 -05:00
eslint-config-next v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
eslint-plugin-next v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next fix NextApiRequestCookies and NextApiRequestQuery types (#25532) 2022-05-23 00:48:26 +00:00
next-bundle-analyzer next-bundle-analyzer: openAnalyzer option (#36493) 2022-05-22 17:23:23 -05:00
next-codemod v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next-env v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next-mdx v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next-plugin-storybook v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next-polyfill-module v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next-polyfill-nomodule v12.1.7-canary.10 2022-05-19 17:11:19 -05:00
next-swc next-swc: Improve error message for nested styled jsx tags (#33805) 2022-05-22 22:45:58 +00:00
react-dev-overlay Fix Trusted Types violations from dangerouslySetInnerHTML on style elements (#36751) 2022-05-22 00:26:54 -05:00
react-refresh-utils v12.1.7-canary.10 2022-05-19 17:11:19 -05:00