Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TypeScript] Can't match a request body if it contains a readonly array #2174

Closed
laurence-myers opened this issue Mar 29, 2021 · 3 comments · Fixed by #2215
Closed

[TypeScript] Can't match a request body if it contains a readonly array #2174

laurence-myers opened this issue Mar 29, 2021 · 3 comments · Fixed by #2215
Labels

Comments

@laurence-myers
Copy link
Contributor

laurence-myers commented Mar 29, 2021

What is the expected behavior?

Can pass an object containing a readonly array as a request matching object.

What is the actual behavior?

TSC compares against each type in the union DataMatcher, fails to match on the type DataMatcherArray, reaches the last type in the union - DataMatcherMap - and raises a confusing error message, masking the root cause.

error TS2345: Argument of type 'Foo' is not assignable to parameter of type 'string | RegExp | Buffer | DataMatcherArray | DataMatcherMap | ((body: any) => boolean) | undefined'.
  Type 'Foo' is not assignable to type 'DataMatcherMap'.
    Index signature is missing in type 'Foo'.

Possible solution

Change the definition of DataMatcherArray, from this:

interface DataMatcherArray extends Array<DataMatcher> {}

To this:

interface DataMatcherArray extends ReadonlyArray<DataMatcher> {}

How to reproduce the issue

interface Foo {
  bar: readonly string[];
}

const requestToMatch: Foo = {
  bar: ['baz']
};

nock(someHost)
  .post(someUrl, requestToMatch) // <-- TSC error occurs here
  .reply(201);

Does the bug have a test case?

No, it affects the type definitions.

Versions

Software Version(s)
Nock 13.0.7
Node v14.15.4
TypeScript 4.1.3
@stale
Copy link

stale bot commented Jul 1, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. We try to do our best, but nock is maintained by volunteers and there is only so much we can do at a time. Thank you for your contributions.

@laurence-myers
Copy link
Contributor Author

I've raised PR #2215 to address this.

@stale stale bot removed the stale label Jul 3, 2021
@gr2m gr2m closed this as completed in #2215 Jul 4, 2021
@github-actions
Copy link

github-actions bot commented Jul 4, 2021

🎉 This issue has been resolved in version 13.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant