You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让VS Code识别重导出时覆盖的JSDoc标签?

问题原因与解决方案

核心问题

这是TypeScript处理重命名导出的JSDoc注释时的一个已知行为限制:当你通过export { _template as template }这种方式重命名导出外部库的函数时,TypeScript的类型检查器在解析完成后,会优先使用原外部库的类型定义,从而忽略你添加在重命名导出上的@deprecated标记——输入过程中VS Code能临时识别是因为编辑器实时语法解析还没完全关联到外部类型,输入完成后类型系统接管就丢失了废弃标记的关联。而直接定义的func不存在这个问题,因为它的类型和JSDoc是完全绑定的。

解决方案

最直接有效的办法是包装原函数而非直接重命名导出,让废弃标记完全绑定到你导出的标识符上:

方案1:直接赋值包装

修改t1.ts为:

import _template from 'lodash/template'; // eslint-disable-line @typescript-eslint/no-restricted-imports

/** @deprecated Deprecated. Use JavaScript template string instead. */
export const template = _template;

/** @deprecated Deprecated. */
export const func = (): void => {};

这种方式和原代码逻辑一致,但通过直接赋值让TypeScript将template的类型和你添加的JSDoc完全关联,输入完成后也会保持废弃划掉样式。

方案2:显式函数包装(更清晰)

如果需要更明确的类型控制,可以用函数包装:

import _template from 'lodash/template';

/** @deprecated Deprecated. Use JavaScript template string instead. */
export function template(...args: Parameters<typeof _template>) {
  return _template(...args);
}

/** @deprecated Deprecated. */
export const func = (): void => {};

这种方式不仅能保留废弃标记,还能更灵活地控制参数和返回值类型,完全规避外部类型的干扰。

验证效果

修改后再打开t2.ts,_.template('')不管是输入过程中还是输入完成后,都会和_.func()一样保持划掉的废弃样式,并且hover时能正确显示你添加的废弃提示。

内容的提问来源于stack exchange,提问作者doberkofler

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 07:07:26