这是我们尝试过的:

{ 
  "compilerOptions": { 
    "target": "esnext", 
    "moduleResolution": "node", 
    "allowJs": true, 
    "jsx": "react" 
  }, 
  "include": [ 
    "src/**/*" 
  ], 
  "exclude": [ 
    "src/**/*.js", 
    "src/**/*.jsx", 
  ] 
} 

当我们从命令行运行 tsc 时,编译器会在 jsxjs 文件中发现错误。例如,我们看到这些错误。

src/components/foo/barHeaderStateOverview.jsx:90:46  
    - error TS8010: 'types' can only be used in a .ts file. 
 
90   generateArbitraryData = (id: string, data: {path: string, title: string}) => { 
                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
 
src/components/foo/barHeaderStateOverview.jsx:101:65  
    - error TS1005: ',' expected. 
 
101     const arbitrary = this.generateArbitraryData('weight', data : (string | number)); 

问题可能是由 this compiler behavior 引起的:

... if a file B.ts is referenced by another file A.ts, then B.ts cannot be excluded unless the referencing file A.ts is also specified in the "exclude" list.

我们的一些 *.ts 文件确实导入了 *.js 和 *.jsx 文件。有没有办法告诉编译器不要对 *.ts 文件导入的那些 *.js 和 *.jsx 文件进行类型检查?

请您参考如下方法:

对于我们来说,这个 tsconfig 文件可以忽略所有 js 和 jsx 文件。

{ 
  "compilerOptions": { 
    "outDir": "build", 
    "module": "esnext", 
    "target": "es5", 
    "lib": ["es6", "dom", "es2016", "es2017"], 
    "sourceMap": true, 
    "checkJs": false, 
    "allowJs": true, 
    "jsx": "react", 
    "moduleResolution": "node", 
    "forceConsistentCasingInFileNames": true, 
    "experimentalDecorators": true, 
    "noImplicitReturns": true, 
    "noImplicitThis": true, 
    "noImplicitAny": true, 
    "strictNullChecks": true, 
    "suppressImplicitAnyIndexErrors": true, 
    "noUnusedLocals": true, 
    "noUnusedParameters": true, 
    "baseUrl": "./", 
    "paths": { 
      "~/*": ["*"] 
    } 
  }, 
  "include": ["./src/**/*.ts", "./src/**/*.tsx"], 
  "exclude": ["node_modules", "build", "dist", "src/**/*.js", "src/**/*.jsx"] 
} 

此外,如果我们在 .jsx|.js 文件中导入 .tsx|.ts 文件,我们必须是明确的。

import someFile from "./path/to/file.tsx" 

我们尚未测试,但如果修改为以下内容,您的可能会运行:

    { 
      "compilerOptions": { 
        "target": "esnext", 
        "moduleResolution": "node", 
        "allowJs": true, 
        "checkJs": false, 
        "jsx": "react" 
      }, 
      "include": [ 
        "./src/**/*.ts", "./src/**/*.tsx" 
      ], 
      "exclude": [ 
        "src/**/*.js", 
        "src/**/*.jsx", 
      ] 
    } 

我希望这有帮助。我们遇到了同样的问题,花了一段时间才让它正常运行


评论关闭
IT虾米网

微信公众号号:IT虾米 (左侧二维码扫一扫)欢迎添加!