我正在使用Typescript,React和Webpack,并努力导入keymirror模块。
我正在使用latest version of typescript。
的package.json
{
"name": "Fun",
"version": "1.0.0",
"description": "Dear Webpack Gods, please help.",
"main": "app.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "",
"dependencies": {
"eventemitter3": "^1.1.1",
"flux": "^2.0.3",
"keymirror": "^0.1.1",
"react": "^0.13.3",
"ts-loader": "^0.5.0",
"typescript": "^1.6.0-dev.20150812"
},
"devDependencies": {
"css-loader": "^0.16.0",
"style-loader": "^0.12.3"
}
}
webpack.config.js
module.exports = {
entry: './App/app.tsx',
output: {
filename: 'bundle.js'
},
resolve: {
extensions: ['', '.js', '.tsx', '.ts']
},
module: {
loaders: [
{ test: /\.ts(x?)$/, loader: 'ts-loader' },
{ test: /\.css$/, loader: 'style-loader!css-loader' }
]
}
}
tsconfig.json
{
"compilerOptions": {
"target": "ES5",
"module": "commonjs",
"jsx": "react",
"sourceMap": true
},
"files": [
"./typings/tsd.d.ts",
"./App/app.tsx"
]
}
违规导入位于./User/Constants.ts
import keyMirror = require('keymirror');
export = keyMirror({
LOGIN_ATTEMPT: null
});
我已尝试使用上面所见的standalone npm module,并尝试从react / lib / keyMirror中请求它。
有什么想法吗?
(编辑:是的,我知道这是一个12行模块,我可以将其复制到我的代码中。)
来自Basarat的解决方案(略有修改)
declare module 'keymirror' {
function keyMirror(obj: Object);
export = keyMirror;
}
答案 0 :(得分:1)
import keyMirror = require(' keymirror');
你需要告诉打字稿。基本上是这样的:
declare module 'keymirror' {
var export:any;
export = export;
}
在名为global.d.ts
的文件中。
一些文档:http://basarat.gitbooks.io/typescript/content/docs/types/ambient/intro.html