Typescript 声明文件在 VSCode 中给我错误

Typescript declaration file giving me error in VSCode

我试图在我的节点模块中添加一个 typescript 声明文件,但是 运行 在 VS Code 的另一个项目中使用它时出现错误:

Exported external package typings file .../ttk.d.ts is not a module

有问题的 ttk.d.ts 文件是 here

奇怪的是,在 VS Code 中,我使用声明文件自动完成。有谁知道我做错了什么?

编辑: 这是导入模块的测试文件:

const ttk = require('ttk');

const t = ttk.factory();
const r = t`Hello, ${'@name'}!`;
const s = r({ name: 'World' });

console.log(s);

基本上文件被声明为全局代码(因为它没有顶级导入或导出),但 TypeScript 期望文件是一个模块。

如果您查看正在引用的 .d.ts,需要将 declare module "ttk" 的内容移出到顶层,以便将文件视为一个模块。

我发送了一个 PR 来解决这里的问题:https://github.com/maexsoftware/ttk/pull/7

希望对您有所帮助!