首页 > 解决方案 > 出现错误:“当提供 '--isolatedModules' 标志时,所有文件都必须是模块”在 React with TypeScript 中

问题描述

我似乎无法摆脱这个错误。我正在使用带有 TypeScript 的 create-react-app。任何帮助,不胜感激。谢谢。

All files must be modules when the '--isolatedModules' flag is provided.  TS1208

  > 1 | import randomcolor from 'randomcolor';
      | ^
    2 | import React from 'react';
    3 | import tasksData from '../../assets/data/tasksData';
    4 | import { Task } from '../../model/task';

这些是我的 tsconfig 文件。我创建了一个基本文件,因为原始 tsconfig 文件被反应构建脚本覆盖。

tsconfig.base.json

{
    "compilerOptions": {
        "isolatedModules": false
    }
}

tsconfig.json

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ],
  "exclude": [
    "node_modules"
  ],
  "extends": "./tsconfig.base.json"
}

标签: reactjstypescript

解决方案


未使用的文件.ts.tsx文件可能会出现此错误。我有没有使用的空.ts文件,所以只需要删除未使用的文件。

删除了未使用的文件/example/index.ts


推荐阅读