typescript - 在自定义定义中引用全局声明的命名空间类型

标签 typescript types namespaces global libraries

过去有一个关于此主题的问题,但没有发布答案:

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目的。
  • https://github.com/DefinitelyTyped/DefinitelyTyped/blob/39580cb71a89cde61d217d8d418347c58a2f2103/types/jest-json-schema/index.d.ts
  • 最佳答案

    代替

    /// <reference types="jest" />
    


    /// <reference types="@types/jest" />
    

    这个对我有用。

    关于typescript - 在自定义定义中引用全局声明的命名空间类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55961831/

    相关文章:

    javascript - Monaco Editor - 如何使某些区域只读

    java - 有没有一种方法可以编写与原始数组类型一起使用的通用 Java 方法?

    python - 避免在多态性不起作用的地方使用 type() 比较

    javascript - 解析 XML 命名空间?

    php - 如何在前端 Controller 情况下使用 PHP 命名空间

    javascript - ChangeDetectionStrategy.OnPush 什么时候实际运行变更检测?

    typescript - 使用 Typescript 和 Jest 添加 i18n,出现 "All files must be modules when the ' --isolatedModules 错误”

    haskell - 为什么 Maybe/Optional 类型使用 Just/Some 类型而不是实际类型?

    php - 如何注册命名空间的自定义 View 助手?

    typescript - 无法使用 typescript 将选项对象添加到 SHA3 函数