question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to infer an interface for `DataMatcher` definition

See original GitHub issue

What is the expected behavior?

The following example should compile:

import * as nock from 'nock';

interface Body {
  param: number;
}

const body: Body = {
  param: 1,
};

const scope = nock('http://example.test')
  .post('/users', body)
  .reply(200);

What is the actual behavior? Typescript does not allow this, because it can not infer an interface as a dictionary. ({ [k: string]: ... }) if it is not explicitly intended as one. More on this issue here https://github.com/microsoft/TypeScript/issues/15300 and here.

The following error is thrown upon compilation:

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

Possible solution

How to reproduce the issue The latest typescript definitions are not yet released which are required for this to reproduce. The example above can be run instead.

Does the bug have a test case?

Versions

Software Version(s)
Nock master
Node 10

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
mastermattcommented, Sep 20, 2019

@jmtelljohann while that would make @JrSchild 's error go away, it would do so at the expense of actually enforcing correct types. Because you can’t actually provide anything you want as values to an object as a body matcher. For instance .post('/', { foo: Buffer.from('bar') }) wouldn’t work, but that type change would appear to allow it.

At this point, I still stand by my suggestion that we should include correct types (as best as TS allows) and if users know for a fact that they’re interfaces are correct then they can cast to any manually.

1reaction
mastermattcommented, Sep 9, 2019

@ctotheameron I agree, the issue is that a root array is currently missing as valid. However, for housekeeping, this is related/an extension of #1703 and is not the same issue @JrSchild is having.

I’d like to spend some time on this fix doing some testing around passing scalars as the body to see if any value that JSON.parse can handle should be considered legit. I just haven’t had the time the last couple of days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why am i unable to infer type parameters from a class that ...
Should it be possible to infer type parameters from a Class that implements an interface or is there something wrong with my implementation?...
Read more >
Unable to infer a output media type as more than one is ...
Solution is to define single output MIME type in DWL script : %dw 2.0; output application/json; --- ...
Read more >
SMILE Wrappers Programmer's Manual
SMILE is a C++ software library for performing Bayesian inference. ... used by the code in the jar file through Java Native Interface...
Read more >
Issues List: /home
46788, Defect, Sample Manager, Unable to create project subfolder in app if "allow users to create/import their own id/names" is unchecked, Guest, 2,...
Read more >
Dmc1 is a candidate for temperature tolerance during ...
Temperature-associated synapsis failure has been reported in plants at ... Ltp was further defined to the long arm of chromosome 5D (Hayter ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found