过去有一个关于此主题的问题,但没有发布答案:
How do you import a Typescript type definition file whose top level element is a non-exported namespace?
我想为 testdouble-jest
做一个类型,但其导出函数中的第二个参数是全局 jest
实例,在 @types/jest
中声明作为顶级声明的命名空间:
declare namespace jest {
mock(): something
// ...
}
编辑:这里是类型:https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts
如何在我的
test-double.d.ts
中引用这个全局对象宣言?我的声明如下所示,但取得了部分成功:/// <reference types="jest" />
// do i actually need the above directive?
declare module "testdouble-jest" {
import * as td from "testdouble"; // the testdouble stuff works
export type TestdoubleJest = typeof td & {
mock: typeof jest.mock; // this actually appears to work
};
function setupTestdoubleJest(
testdouble: typeof td,
jest: typeof jest // this just resolves to any
): TestdoubleJest;
export = setupTestdoubleJest;
}
如果有人知道如何做到这一点,我将不胜感激!
编辑2:有一些关于DefinitelyTyped的例子,要么增加'jest'命名空间或使用它的成员(就像我对
jest.mock
所做的那样),但我找不到一个指的是实际的全局jest
目的。最佳答案
代替
/// <reference types="jest" />
和
/// <reference types="@types/jest" />
这个对我有用。
关于typescript - 在自定义定义中引用全局声明的命名空间类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55961831/