TypeScript错误"无法读取属性' exports'未定义"

时间:2017-01-18 08:39:29

标签: typescript compiler-errors webpack react-redux typescript2.0

我正在尝试使用TypeScript构建Redux中间件。这个功能

import { Middleware } from "redux"

function createMiddleware() {
  return ({ dispatch, getState }) => next => action => {
    if (Math.random() > 0.5) {
      return next(action)
    }

    return next(action)
  }
}

export const middleware: Middleware = createMiddleware()

产生TypeScript错误:

Typescript error: Cannot read property 'exports' of undefined

虽然删除if-block时编译器没有抱怨。

我的tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "ES5",
    "noImplicitAny": false,
    "skipLibCheck": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "jsx": "react",
    "lib": ["dom", "es2015", "es2016"],
    "baseUrl": "../src",
    "exclude": [
      "node_modules"
    ]
  }
}

编辑来自浏览器控制台的Stacktrace

Uncaught Error: Module build failed: TypeError: Cannot read property 'exports' of undefined
    at resolveName (..\node_modules\typescript\lib\typescript.js:24948:70)
    at getResolvedSymbol (..\node_modules\typescript\lib\typescript.js:32549:67)
    at checkIdentifier (..\node_modules\typescript\lib\typescript.js:33700:26)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38185:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at checkPropertyAccessExpressionOrQualifiedName (..\node_modules\typescript\lib\typescript.js:35587:24)
    at checkPropertyAccessExpression (..\node_modules\typescript\lib\typescript.js:35554:20)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38208:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at resolveCallExpression (..\node_modules\typescript\lib\typescript.js:36556:28)
    at resolveSignature (..\node_modules\typescript\lib\typescript.js:36786:28)
    at getResolvedSignature (..\node_modules\typescript\lib\typescript.js:36809:26)
    at checkCallExpression (..\node_modules\typescript\lib\typescript.js:36836:29)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38213:28)
    at resolveName (..\node_modules\typescript\lib\typescript.js:24948:70)
    at getResolvedSymbol (..\node_modules\typescript\lib\typescript.js:32549:67)
    at checkIdentifier (..\node_modules\typescript\lib\typescript.js:33700:26)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38185:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at checkPropertyAccessExpressionOrQualifiedName (..\node_modules\typescript\lib\typescript.js:35587:24)
    at checkPropertyAccessExpression (..\node_modules\typescript\lib\typescript.js:35554:20)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38208:28)
    at checkExpression (..\node_modules\typescript\lib\typescript.js:38165:42)
    at checkNonNullExpression (..\node_modules\typescript\lib\typescript.js:35540:24)
    at resolveCallExpression (..\node_modules\typescript\lib\typescript.js:36556:28)
    at resolveSignature (..\node_modules\typescript\lib\typescript.js:36786:28)
    at getResolvedSignature (..\node_modules\typescript\lib\typescript.js:36809:26)
    at checkCallExpression (..\node_modules\typescript\lib\typescript.js:36836:29)
    at checkExpressionWorker (..\node_modules\typescript\lib\typescript.js:38213:28)
    at Object../src/store/middleware.ts (http://localhost:8080/static/js/main.js:69038:7)
    at __webpack_require__ (http://localhost:8080/static/js/main.js:660:30)

1 个答案:

答案 0 :(得分:0)

感谢Martin,我发现它实际上是一个Webpack 2问题。切换到Webpack 1为我解决了它。