使用tsconfig.json

时间:2018-10-22 22:37:00

标签: reactjs typescript tsc

这是我们尝试过的:

{
  "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造成的:

  

...如果文件B.ts被另一个文件A.ts引用,则不能排除B.ts,除非在“排除”列表中也指定了引用文件A.ts。

我们的某些* .ts文件确实导入了* .js和* .jsx文件。有没有办法告诉编译器不要键入检查* .ts文件导入的* .js和* .jsx文件?

1 个答案:

答案 0 :(得分:6)

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

{
Country: United States of America
... blah blah
... blah blah
... blah blah
... blah blah
}

fs.createReadStream("./csv/03312020.csv")
    .pipe(
        csv([
            "Country",
            "Total",
            "TotalNew"
        ])
    )
    .on("data", row => {
        console.log(row.Country);
        let result = contains(row.Country);
        if (result !== undefined) {
            row.Date = today;
            row.id = result + "-" + today;
            if (db.dates.get(row.id) === undefined) db.dates.create(row);
        }
    })
    .on("end", () => {
        console.log("CSV file successfully processed for", today);
    });

另外,如果我们将.tsx | .ts文件导入.jsx | .js文件,则必须明确。

{
  "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"]
}

我们尚未测试,但如果修改为:

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

我希望这会有所帮助。我们遇到了同样的问题,花了一段时间让它正确进行