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

Types for RequestHandler are too strict #321

Closed
mrijke opened this issue Jun 7, 2021 · 0 comments
Closed

Types for RequestHandler are too strict #321

mrijke opened this issue Jun 7, 2021 · 0 comments

Comments

@mrijke
Copy link
Contributor

mrijke commented Jun 7, 2021

With the rewrite to TypeScript, the API as described in the README does not work in TypeScript projects.
The following:

const server = new Pretender(function() {
  this.put('/api/songs/99', request => [404, {}, ""]);
});

fails with the following TS error:

Argument of type '(request: FakeXMLHttpRequest & ExtraRequestData) => [number, {}, string]' is not assignable to parameter of type 'ResponseHandler'.
  Type '(request: FakeXMLHttpRequest & ExtraRequestData) => [number, {}, string]' is missing the following properties from type 'ResponseHandler': async, numberOfCallsts(2345)

The extra attributes that were introduced on the handler instance (async and numberOfCalls) are also mandatory to provide when defining the mock itself - which I think is not intended, these are meant to be (readonly) properties, correct?

For now the following workaround seems to make TS happy - but it's not very ideal:

const handler = (): ResponseData => [
  200,
  { "Content-Type": "applicatoin/json" },
  JSON.stringify({ status: "OK!" }),
];
handler.async = false;
handler.numberOfCalls = 0;
xg-wang added a commit that referenced this issue Jul 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant