Typescript import index.ts不是模块

时间:2017-07-06 07:42:51

标签: javascript reactjs typescript import export

我正在尝试在包含其他导入的子文件夹中导入index.ts。但我不断收到打字稿错误。

完整回购:https://github.com/Shavindra/webpack-react-sw

(5,32): error TS2306: File 'C:/../src/reducers/index.ts' is not a module.

我不太清楚我在这里做错了什么。我使用的是TS 2.4.1。我已经尝试重新启动计算机/ VSCode,但似乎没有任何工作: - |

// ./src/reducers/counter.reducer.ts    
export const counterReducer = (state = 0, action) => {
  switch (action.type) {
    case 'INCREMENT':
      return state + 1;
    case 'DECREMENT':
      return state - 1;
    default:
      return state;
  }
};


// ./src/reducers/index.ts
export * from './counter.reducer';


// ./src/app.ts
import * as React from 'react';
import * as React from 'react';
import * as ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Counter } from './components/counter/counter.component';
import { counterReducer } from './reducers';


const store = createStore(counterReducer);
const rootEl = document.getElementById('root');

const render = () => ReactDOM.render(
    <Counter
        value={store.getState()}
        onIncrement={() => store.dispatch({ type: 'INCREMENT' })}
        onDecrement={() => store.dispatch({ type: 'DECREMENT' })}
    />,
    rootEl
);

render();
store.subscribe(render);

// tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es5",
    "sourceMap": true,
    "jsx":"react",
    "lib": [
      "webworker",
      "es6",
      "scripthost",
      "dom"
    ]
  },
  "files": [ "node_modules/@types/react-dom/index.d.ts", "node_modules/@types/react/index.d.ts", "typings/file-loader.d.ts"  ],
  "exclude": [
    "typings/browser.d.ts",
    "typings/browser",
    "node_modules"
  ]
}

1 个答案:

答案 0 :(得分:1)

有些想法是:

  • reducers是一个文件夹,tsc正在尝试搜索./reducers/index.ts
  • reducers是一个文件,tsc
  • 无法读取
  • recuders是一个有效的文件,但具有不同的导出系统。检查UMD,systemjs,commonjs包括

您可以通过以下方式加入: import counterReducer = require('./recuders'); import * as counterReducer from './recuders';

如果您想通过import { mod } from 'file';包含单个模块,请确保在reducers.ts中导出此函数,类或任何您想要的内容

我希望这些想法有所帮助。让我知道!

您能否发布您的tsconfig.json以确保一切设置正确?