为现有模块创建d.ts文件 [英] Creating a d.ts file for existing module

查看:101
本文介绍了为现有模块创建d.ts文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为 React StaticContainer 库创建d.ts文件

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

NPM中已安装的库如下:

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;

用法示例如下:

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

我不确定如何为此创建声明并在TypeScript中使用它.到目前为止,我还没有提出以下建议:

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'

这个d.ts文件:

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

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

但是TSC给我这个错误:

However TSC gives me this error:

错误:(3,34)TS2497:模块'"react-static-container"'解析为非模块实体,无法使用此构造导入.

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

我很困惑module.exports = StaticContainer应该如何转换为d.ts文件.怎么做?

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

推荐答案

模块'"react-static-container"'解析为非模块实体,无法使用此构造导入.

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

确切说错了.您需要使用import require导入export =样式库.这是因为ES6规范.

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');

这篇关于为现有模块创建d.ts文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆