Typescript import index.ts 不是模块

Mon*_*sto 5 javascript import export typescript reactjs

我正在尝试将 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"
  ]
}
Run Code Online (Sandbox Code Playgroud)

小智 2

一些想法是:

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