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.

How do you add methods when using typescript?

See original GitHub issue

I believe I might have found an issue with how cheerio exports types, but I’m new to typescript, so I’m not sure I’m not just missing how to get it done.

I’m running v1.0.0-rc.5, and I’d like to be able to add some methods to the Cheerio interface, so that when I run a selector query, I get back the full api (both mine and the core methods). I had better luck using @types/cheerio, as it seemed to expose more of the API, but I wasn’t able to get it fully working there either. For that reason, I’m not sure #1682 is going to fully solve the problem.

I’d like to be able to include a code sample, but working against v1, nothing I came up with is close. If it is possible with the current release, I’d just like to know how to do it. Thanks!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
mikestopcontinuescommented, Mar 16, 2021

Hi, this issue should be re-opened. I don’t know how #1407 got it working. Perhaps he was using @types/cheerio? Merging doesn’t work on any level (global, module, namespace). Any attempt just overrides the base interfaces. I can’t be sure, but I suspect the issue is that the interfaces aren’t being exported. In the examples here, they are exporting everything they merge.

0reactions
mikestopcontinuescommented, Jun 13, 2021

Just want to add, you need to include export {} (or export anything) in your .d.ts file for the new method to work. Otherwise the custom definition overrides the basic export types.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Is there a way to add methods on the fly to a class using ...
This works great for adding static methods to js classes but I was looking to add methods to the prototype so they would...
Read more >
Functions - TypeScript: Handbook
Functions are the fundamental building block of any application in JavaScript. They're how you build up layers of abstraction, mimicking classes, ...
Read more >
How To Use Functions in TypeScript - DigitalOcean
In this section, you will create functions in TypeScript, and then add type information to them. ... In this example, sum is the...
Read more >
I want to add methods to Object : r/typescript - Reddit
I want to add methods to Object. Is there a convention for providing additional methods to existing classes (like you can with obj.member ......
Read more >
TypeScript Interfaces - TutorialsTeacher
An interface is defined with the keyword interface and it can include properties and method declarations using a function or an arrow function....
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