3

I'm trying to create a d.ts file for the React StaticContainer library.

The installed lib in NPM looks like this:

var React = require('react');

var StaticContainer = (function (_React$Component) {

  function StaticContainer() {
    // ...
  }

  // ...

  return StaticContainer;
})(React.Component);

module.exports = StaticContainer;

An example usage is like this:

var StaticContainer = require('react-static-container');

And I'm not sure how to create a declaration for this and use it in TypeScript. So far from my research I've come up with this:

import * as StaticContainer from 'react-static-container'

And this d.ts file:

interface StaticContainer extends React.Component<any, any> {
  shouldUpdate?:boolean;
}

declare module "react-static-container" {
  export = StaticContainer;
}

However TSC gives me this error:

Error:(3, 34) TS2497: Module '"react-static-container"' resolves to a non-module entity and cannot be imported using this construct.

I'm rather confused how the module.exports = StaticContainer should translate to a d.ts file. How is this done?

1 Answer 1

1

Module '"react-static-container"' resolves to a non-module entity and cannot be imported using this construct.

Exactly what the error says. You need to import export = style libraries with import require. This is because of the ES6 spec.

import StaticContainer =  require('react-state-container');
Sign up to request clarification or add additional context in comments.

3 Comments

I'm sure it means what is says, most error messages do ;) but I don't have the context to understand it... "this is because of the ES6 spec" unfortunately doesn't further my understanding. Can you elaborate? The code you posted doesn't work, now I get TS2304: Cannot find name 'StaticContainer'. I'm guessing my d.ts does not correctly reflect the way the original library is exported.
@Aaron got exactly same issue. had to define the same name before as var. Eg, let StaticContainer : any in this case. Don't sure why it happens.
ES6 spec allows you to export individual items. Exporting an object is not statically analyzable and not supported.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.