React 中的 tsc compile (create-react-app) 需要明确的编译标志,尽管我在 tsconfig.json 中设置了它们

tsc compile in React (create-react-app) needs explicit compile flags altough I set them in tsconfig.json

我首先使用“create-react-app”初始化了一个 React 应用程序,然后使用 yarn add typescript @types/node @types/react @types/react-dom @types/jest 我用默认值初始化了 tsconfig.json:

{
  "compilerOptions": {
    "target": "es5",
    "module": "esnext",
    "allowJs": true,
    "jsx": "react-jsx",
    "sourceMap": true,
    "outDir": "./dist",
    "noEmit": true,
    "isolatedModules": true,
    "strict": true,
    "moduleResolution": "node",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "noFallthroughCasesInSwitch": true,
    "resolveJsonModule": true
  },
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ],
  "include": [
    "./src/**/*.tsx"
  ]
}

如果我尝试编译像 tsc App.tsx 这样的单个文件,我会收到错误提示我需要使用 --jsx 标志和 --esModuleInterop 标志。但是我已经在 tsconfig.json 中设置了它们,为什么我必须在命令行中明确指定它们? 它只能这样工作:tsc index.tsx --jsx preserve --esModuleInterOp 但是为什么?

来自docs

tsc index.ts

emits JS for just the index.ts with the compiler defaults (not the project's tsconfig)

而且,您不能 specify file/s to compile and the tsconfig file 在 CLI 上:

tsc index.ts tsconfig.json // NOT ALLOWED

因此,如果您使用 CLI 仅编译一个或多个文件,您必须提供选项(在本例中为 jsx 和 esModuleInterOp)或 create new tsconfig file that extends the existing one, and add your file to it