简体   繁体   中英

Typescript add missing methods to exported library class

In the snoowrap library they have a custom .d.ts file , but the exported Snoowrap class is missing a getContentByIds() method. How can I add that method in my own type declaration file?

I tried multiple SO answers, like this one , and created my own snoowrap.d.ts but that doesn't work:

// snoowrap.d.ts
import snoowrap, { Comment, Submission, Listing } from 'snoowrap';

declare module 'snoowrap' {
    export default interface Snoowrap extends snoowrap {
        getContentByIds(ids: Array<string | Submission | Comment>): Promise<Listing<Submission | Comment>>
    }
}

My code:

// main.ts
import snoowrap from 'snoowrap';
const reddit = new snoowrap();
const submissions = await reddit.getContentByIds(['t3_9l9vof']);

My error:

Property 'getContentByIds' does not exist on type 'import("project/node_modules/snoowrap/dist/snoowrap.d.ts")'.ts(2339)

If you check the content of node_modules/snoowrap/dist/snoowrap.d.ts you'll see that the class Snoowrap is not defined within a namespace , also it doesn't have an interface. So I think in order to override this class with new functions you should try the snipped below:

// snoowrap.d.ts
import {default as SnoowrapClass, Comment, Submission, Listing } from 'snoowrap';

export default class Snoowrap extends SnoowrapClass {
    getContentByIds(ids: Array<string | Submission | Comment>): Promise<Listing<Submission | Comment>>
}

and then import from the file above where you want to use it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM