【发布时间】:2019-10-27 00:47:48
【问题描述】:
我用我的参数类型和函数的全局声明创建了一个 .d.ts 文件。 只要全局函数是用 javascript 编写的,就可以正常工作。
当转换为打字稿时,我引用了 .d.ts 文件来导入类型。
但是在编译时会抱怨声明和实现的函数,又名error TS2384: Overload signatures must all be ambient or non-ambient.
- 为什么我不能在声明中引用我的 d.ts 文件? / 为什么我不允许声明在 typescript 中实现的函数?
在旧 C 中,可以将 h 文件包含在当前文件的声明中,而且通常非常有用。
- 我应该把实现、接口和函数声明放在哪里,我应该如何在它们之间引用以避免错误? (因为我目前为什么将它们拆分为 .ts 和 .d.ts 文件是不正确的)
做了一个缩小的例子:
// test.d.ts
interface test_option {
a: boolean;
}
declare function test(options: test_option): boolean;
// test.ts
/// <reference path="test.d.ts" />
function test(options: test_option): boolean {
return !options.a;
}
// test2.ts
/// <reference path="test.d.ts" />
window.console.log(test({a: true}));
【问题讨论】:
-
不确定是否有理由要创建 d.ts 然后在打字稿中实现它。您为 javascript 文件创建 d.td。 ts 不需要它......如果你想拥有 d.ts 你仍然可以设置“声明”:true。在 tsconfig 中,因此编译器将为您创建自定义 d.ts 文件
标签: typescript