微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

覆盖现有库的类方法声明

如何解决覆盖现有库的类方法声明

我目前正在尝试为名为 markdown-it-Meta 的 Markdown-it 插件库创建类型声明文件

markdown-it 类有一个叫做 use 的函数,它基本上接受一个回调函数,然后改变 this 关键字并返回 void。 (我理解代码的方式)。

问题在于,提供上述回调函数的库实际上向 this 对象添加一个属性

现在我正在寻找一种方法来在调用 use 函数时正确更新 this 关键字。

这是一个例子:

const parser = new MarkdownIt({html:true});
parser.use(Meta)

const buffer = fs.readFileSync('test.md','utf-8');
const post = parser.render(buffer);
const MetaData = parser.Meta; <- **THIS GIVES ERROR**

到目前为止我所拥有的:

// FROM markdown-it lib's own declaration file as a reference for the code below
//    type PluginSimple = (md: MarkdownIt) => void;

// what I have 
// -- markdown-it-Meta.d.ts
declare module 'markdown-it-Meta' {
    import {PluginSimple} from 'markdown-it'

    const MetaPlugin: PluginSimple
    export default MetaPlugin;
}

我的一些想法:

  • 我看到 markdown-it 类型声明文件有 3 种不同的函数重载类型,它们都返回 this。也许我可以做一个新的重载,每当我们接收类型为 markdown-it-Meta-plugin-function 的函数时,它都会返回 this & {Meta: T}。但是我会把这些放在哪里,怎么放?
  • 我的另一个想法是在 markdown-it-Meta.d.ts 中创建一种新类型的 MetaPlugin,但是我不知道如何在函数中改变 this 关键字。特别是因为它必须返回 void。

非常感谢任何帮助或建议。

提前致谢。

两个库的链接

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。