1
votes

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.

1

1 Answers

0
votes

replace

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

with

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

it works for me.