Refer to global declared namespace type in custom definition

家住魔仙堡 提交于 2020-01-15 09:07:30

问题


There is a past question on this topic, but no answer was posted:

How do you import a Typescript type definition file whose top level element is a non-exported namespace?

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
  // ...
}

EDIT: here are the types: https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/jest/index.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!

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.

  • https://github.com/DefinitelyTyped/DefinitelyTyped/blob/39580cb71a89cde61d217d8d418347c58a2f2103/types/jest-json-schema/index.d.ts

回答1:


replace

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

with

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

it works for me.



来源:https://stackoverflow.com/questions/55961831/refer-to-global-declared-namespace-type-in-custom-definition

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!