2016-07-26 59 views
1

我已從DefinitelyTyped中拉取react.d.ts文件,並將其作爲node_modules/react/react.d.ts包含在我的項目中。現在,當我寫import * as React from "node_modules/react/react",我得到以下錯誤:使用DefinitelyTyped定義時TypeScript:TS2036「不是模塊」錯誤

Error TS2306: File '.../node_modules/react/react.d.ts' is not a module. 

我可以在react.d.ts解決此通過更改以下行:

declare module "react" { 
    export = __React; 
} 

到:

export = __React; 

我問題是:使用TypeScript定義文件的建議工作流程是什麼?

+0

你爲什麼要明確給出節點模塊內的文件路徑?您應該只需要'import *作爲來自「react」的React' – cdbajorin

+0

然後RequireJS如何知道在哪裏可以找到我的模塊? –

+0

你在'require.config({paths:{...}}}中告訴它路徑' – cdbajorin

回答

2

隨着新的TypeScript 2.0仍處於測試階段(npm install [email protected] -g),您應該使用npm install @types/react來獲取您的定義文件。 其次,如果有一個模塊在一份聲明中export =(或一個模塊的聲明),你應該相應語法import =像導入以下:import React = require('react');

相關問題