TypeScript: is there a way to use the JSDOC @deprecated tag to mark a symbol imported from a module as deprecated?

huangapple go评论62阅读模式
英文:

TypeScript: is there a way to use the JSDOC @deprecated tag to mark a symbol imported from a module as deprecated?

问题

我想在从第三方模块导入时将一个符号标记为已弃用。
在以下示例中,bar 是一个 npm 模块,它导出函数 foo,我想将 foo 标记为已弃用,以通知不应再使用此 API。

英文:

I would like to mark a symbol as deprecated when imported from an 3rd party module.
In the following example bar is some npm module that exports the function foo and I would like to mark foo as deprecated to inform that this api should no longer be used.

import {foo} from 'bar';

foo();

答案1

得分: 1

据我所了解,@deprecated 标签必须写在导入符号的声明位置。

我尝试创建了两个测试仓库,并使用 declare module... 进行声明合并,但出现了以下错误:

> 无法重新声明具有块范围的变量 'foo'。ts(2451)

作为解决方法,您可以在自己的模块中导入该函数并将其分配给一个新变量,应用任何您想要的文档,然后重新导出以在其他模块中使用。例如:

./src/bar.ts:

import { foo as fooImport } from "bar";

/** @deprecated */
export const foo: typeof fooImport = fooImport;

./src/another_module.ts:

import { foo } from "./bar";

foo();
//^? 将具有 @deprecated JSDoc
英文:

As far as I can tell, the @deprecated tag must be written at the declared site of the symbol that's imported.

I tried creating two test repos and used declaration merging with declare module..., but got the following error:

> Cannot redeclare block-scoped variable 'foo'. ts(2451)

As a workaround, you can import the function in your own module and assign it to a new variable, applying whatever documentation you'd like — then re-export it for use in your other modules. For example:

./src/bar.ts:

import { foo as fooImport } from "bar";

/** @deprecated */
export const foo: typeof fooImport = fooImport;

./src/another_module.ts:

import { foo } from "./bar";

foo();
//^? Will have the @deprecated JSDoc

huangapple
  • 本文由 发表于 2023年4月20日 01:26:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76057292.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定