TypeDoc如何链接Array、Promise等TypeScript内置类型?
TypeDoc链接TypeScript内置类型方法的问题
问题描述
我无法找到引用TypeScript内置类型(例如TypeScript自带的lib.es5.d.ts中的类型)的方法。例如我编写了一个stringSplice函数,功能与原生Array.prototype.splice基本一致,希望在注释中通过@link链接到该内置方法,但尝试了多种写法:
/** * Works like {@link Array.prototype.splice} * Works like {@link Array.splice} * Works like {@link Array:splice} * Works like {@link Array::splice} * Works like {@link Array#splice} */ export const stringSplice = (str: string, start = 0, deleteCount?: number, replacement = "") => { start = start < 0 ? mod(start, str.length) : start; deleteCount = Math.max(deleteCount ?? str.length - start, 0); return str.slice(0, start) + replacement + str.slice(start + deleteCount); };
每次都提示Failed to resolve link to xyz错误。请问是否支持此类链接?或者是否需要安装插件?
附当前TypeDoc配置:
{ "entryPointStrategy": "packages", "entryPoints": ["packages/*"], "out": "./docs", "plugin": ["typedoc-plugin-resolve-crossmodule-references"] }
解决方案
- TypeDoc默认不支持直接链接到TypeScript内置的
lib.d.ts中的符号,因为这些不属于你项目代码定义的实体,所以常规写法会解析失败。 - 可以尝试使用全局对象前缀的写法:
{@link global.Array.prototype.splice},部分TypeDoc版本能识别这种全局符号的链接。 - 更可靠的方式是安装专门的插件
typedoc-plugin-mdn-links,它能自动处理内置API的链接解析,避免报错。安装后更新TypeDoc配置:{ "entryPointStrategy": "packages", "entryPoints": ["packages/*"], "out": "./docs", "plugin": ["typedoc-plugin-resolve-crossmodule-references", "typedoc-plugin-mdn-links"] }
内容的提问来源于stack exchange,提问作者Benjamin M
相关产品推荐
相关产品推荐

