Skip to content

fix: authorize undefined values in DataMatcher type#1723

Closed
jgiovaresco wants to merge 1 commit intonock:masterfrom
jgiovaresco:fix-datamatcher-ts-definition
Closed

fix: authorize undefined values in DataMatcher type#1723
jgiovaresco wants to merge 1 commit intonock:masterfrom
jgiovaresco:fix-datamatcher-ts-definition

Conversation

@jgiovaresco
Copy link
Copy Markdown

When the type used in .query defines optional keys using undefined, TypeScript was complaining :

Error:(59, 19) TS2345: Argument of type '{ a: string; b?: string | undefined; }' is not assignable to parameter of type 'string | boolean | DataMatcher | URLSearchParams | ((parsedObj: ParsedUrlQuery) => boolean)'.
  Type '{ a: string; b?: string | undefined; }' is not assignable to type 'DataMatcher'.
    Property 'b' is incompatible with index signature.
      Type 'string | undefined' is not assignable to type 'string | number | boolean | RegExp | DataMatcher | (string | number | boolean | RegExp | DataMatcher | null)[] | null'.
        Type 'undefined' is not assignable to type 'string | number | boolean | RegExp | DataMatcher | (string | number | boolean | RegExp | DataMatcher | null)[] | null'.

When the type used in `.query` defines optional keys using undefined, TypeScript was complaining :

```
Error:(59, 19) TS2345: Argument of type '{ a: string; b?: string | undefined; }' is not assignable to parameter of type 'string | boolean | DataMatcher | URLSearchParams | ((parsedObj: ParsedUrlQuery) => boolean)'.
  Type '{ a: string; b?: string | undefined; }' is not assignable to type 'DataMatcher'.
    Property 'b' is incompatible with index signature.
      Type 'string | undefined' is not assignable to type 'string | number | boolean | RegExp | DataMatcher | (string | number | boolean | RegExp | DataMatcher | null)[] | null'.
        Type 'undefined' is not assignable to type 'string | number | boolean | RegExp | DataMatcher | (string | number | boolean | RegExp | DataMatcher | null)[] | null'.
```
@mastermatt mastermatt added the TypeScript Anything related to TypeScript label Sep 19, 2019
@mastermatt
Copy link
Copy Markdown
Member

mastermatt commented Sep 19, 2019

Thanks for this @jgiovaresco, this is definitely a bug.
There are, however, a couple other issues open atm with the DataMatcher type (#1724).

I'd like to hold off on merging this PR while I continue testing my in-progress work. But I am going to borrow your test file changes to make sure this use case is covered.

mastermatt added a commit to mastermatt/nock that referenced this pull request Sep 19, 2019
Fixes: nock#1724
Closes: nock#1723

Updates the JSONish type using inspiration from the thread on adding a
JSON type to TS. microsoft/TypeScript#1897 (comment)

This change does a couple sublet things:
- Adds `undefined` to scalar values allowed in arrays or as values in objects.
  This allows for interfaces with optional keys to be passed in. ref nock#1723
- Splits out top-level array and objects into their interfaces so they could be used directly.
  Allows enforcing non-arrays for `.query`.
- `RequestBodyMatcher` now extends the array and map types instead of the raw `DataMatcher`.
  Allowing arrays fixes nock#1724.
  This also ensures that booleans, numbers, and `null` are not considered valid.
mastermatt added a commit that referenced this pull request Sep 20, 2019
* fix(types): data matcher for body and query.

Fixes: #1724
Closes: #1723

Updates the JSONish type using inspiration from the thread on adding a
JSON type to TS. microsoft/TypeScript#1897 (comment)

This change does a couple sublet things:
- Adds `undefined` to scalar values allowed in arrays or as values in objects.
  This allows for interfaces with optional keys to be passed in. ref #1723
- Splits out top-level array and objects into their interfaces so they could be used directly.
  Allows enforcing non-arrays for `.query`.
- `RequestBodyMatcher` now extends the array and map types instead of the raw `DataMatcher`.
  Allowing arrays fixes #1724.
  This also ensures that booleans, numbers, and `null` are not considered valid.
@nockbot
Copy link
Copy Markdown
Collaborator

nockbot commented Sep 20, 2019

🎉 This issue has been resolved in version 11.3.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

juninmd pushed a commit to juninmd/nock that referenced this pull request Mar 21, 2026
* fix(types): data matcher for body and query.

Fixes: nock#1724
Closes: nock#1723

Updates the JSONish type using inspiration from the thread on adding a
JSON type to TS. microsoft/TypeScript#1897 (comment)

This change does a couple sublet things:
- Adds `undefined` to scalar values allowed in arrays or as values in objects.
  This allows for interfaces with optional keys to be passed in. ref nock#1723
- Splits out top-level array and objects into their interfaces so they could be used directly.
  Allows enforcing non-arrays for `.query`.
- `RequestBodyMatcher` now extends the array and map types instead of the raw `DataMatcher`.
  Allowing arrays fixes nock#1724.
  This also ensures that booleans, numbers, and `null` are not considered valid.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

released TypeScript Anything related to TypeScript

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants