我正在尝试在具有其他导入内容的子文件夹中导入index.ts。但是我一直收到 typescript 错误。

完整回购: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"
  ]
}

最佳答案

一些想法是:

  • 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来确保一切都正确设置?

    10-05 18:16