问题描述
这个主题有一个过去的问题,但未发布答案:
There is a past question on this topic, but no answer was posted:
我想为testdouble-jest
创建类型,但是其导出函数中的第二个参数是全局jest
实例,该实例在@types/jest
中声明为顶级声明的命名空间:
I want to make a type for testdouble-jest
, but the second argument in its exported function is the global jest
instance, which is declared in @types/jest
as a top-level declared namespace:
declare namespace jest {
mock(): something
// ...
}
以下是这些类型: https: //github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.d.ts
如何在test-double.d.ts
声明中引用此全局对象?我的声明如下,但部分成功:
How can I refer to this global object in my test-double.d.ts
declaration? My declaration looks as follows, but with partial success:
/// <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;
}
如果有人知道该怎么做,我将不胜感激!
If anyone knows how to do this, I would really appreciate it!
编辑2:有一些关于DefinitelyTyped的人的例子,它们要么增加了'jest'名称空间,要么使用了它的成员(就像我对jest.mock
所做的那样),但是我找不到一个引用实际全局jest
对象.
EDIT 2: There are some examples on DefinitelyTyped of people either augmenting the ‘jest‘ namespace or using its members (like I did with jest.mock
), but I can't find one referring to the actual global jest
object.
推荐答案
替换
/// <reference types="jest" />
使用
/// <reference types="@types/jest" />
对我有用.
这篇关于在自定义定义中引用全局声明的名称空间类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!