[英]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.在snoowrap库中,他们有一个自定义的.d.ts 文件,但导出的 Snoowrap 类缺少
getContentByIds()
方法。 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:我尝试了多个 SO 答案,比如这个,并创建了我自己的
snoowrap.d.ts
但这不起作用:
// 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.如果您检查
node_modules/snoowrap/dist/snoowrap.d.ts
的内容,您将看到类Snoowrap
未在namespace
定义,也没有接口。 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.然后从上面要使用的文件中导入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.