2015-11-20 134 views
2

我正在尝试为React StaticContainer库创建d.ts文件。为现有模块创建d.ts文件

在NPM安装的LIB是这样的:

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中。到目前为止,从我的研究,我想出了这一点:

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给了我这个错误:

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文件困惑。这是如何完成的?

回答

1

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

究竟是什么错误说。您需要导入export =样式库与import require。这是因为ES6规范。

import StaticContainer = require('react-state-container'); 
+1

我确定这意味着什么,大多数错误消息都会这样做;但是我没有上下文来理解它......“这是因为ES6规范”不幸的是没有进一步我的理解。你能详细说明吗?您发布的代码不起作用,现在我得到'TS2304:找不到'StaticContainer'。'我猜我的'd.ts'不能正确反映原始库的输出方式。 – Aaron

+0

@Aaron得到了完全相同的问题。必须在var之前定义相同的名称。例如,在这种情况下,让'StaticContainer:any'。不知道为什么会发生。 – Guria

+0

如何在这种情况下使用socket.io @basarat – nikoss