empz
empz

Reputation: 11828

TypeScript: How to import a typed module in a d.ts file (in a npm package)?

We're trying to include a definition file in a npm package that depends on React.

The library is https://github.com/eiriklv/react-masonry-component.

Providing the following definition in a custom d.ts file in a TypeScript project works fine:

declare module "react-masonry-component" {
  import React = __React;

  interface MasonryPropTypes {
    disableImagesLoaded: boolean;
    options: Object;
    className: string;
    elementType: string
  }

  export var Masonry: React.Component<MasonryPropTypes, void>;
}

The problem is that the same definition inside a d.ts file in the package itself (with the proper typings key set in package.json) doesn't work because it doesn't recognize the __React type which is provided via tsd/typings from DefinitelyTyped.

How's the correct approach for this? Do we have to duplicate the declaration for React just to make the compiler happy or is there a way to include the React.Component?

Upvotes: 2

Views: 898

Answers (1)

cantide5ga
cantide5ga

Reputation: 11

Not enough reputation to comment, but wanted to see if you got anywhere with this? It doesn't look like you are using Typings or any other typedef manager here and are relying solely on tsd's module resolution. Correct?

I mentioned this question in this issue: https://github.com/typings/typings/issues/645

Think we have stumbled on the same thing here and will edit with a resolution.

EDIT: I was incorrect in the comment about you not using a typedef manager. You clearly stated it, though I don't see anything definitively in your repo.

Cliffnotes for getting this all to work they way you want: I suggest you use Typings as the manager; removing the typings entry from your package.json and instead pointing to it in a typings.json will allow the import to resolve.

Upvotes: 1

Related Questions