如何在TypeScript中动态生成文档字符串?



是否有一种方法可以从TypeScript中的对象值动态生成注释/文档字符串?

我有一个translations.json文件,看起来像这样:

{
"hello": { "en": "hello", "de": "hallo" },
"bye": { "en": "bye", "de": "tschüss" }
}

和一个translate.ts看起来像这样:

import type translations from './translations.json';
type TranslationKey = keyof typeof translations;
function translate(key: TranslationKey){
// some translation code
}

当我使用translate()函数时,我得到JSON-Object的所有键作为完成建议。

我的问题是:是否有可能从json属性的值动态生成某种文档字符串?这样,编辑器的建议弹出框就会显示这些值作为附加信息。

编辑器中的期望结果:

translate('<cursor>
╭───────╮╭───────────────╮
│bye   >├┤bye            │
│hello  ││───────────────│
╰───────╯│TranslationKey │
│en: bye        │
│de: tschüss    │
╰───────────────╯

这些JSON补全项是由vscode JSON补全提供程序生成的,它不能被配置为以特定的方式构造这些项。修改补全输出的唯一方法是编写自己的补全提供程序(对于这样简单的结果来说,这可能工作量太大了)。

最新更新