问题描述
我目前正在尝试为名为 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 (将#修改为@)