我现在正在做一个非常新鲜的项目。 这是盖茨比反应/打字稿。
虽然我的 IDE 正在识别并显示打字稿错误,但代码正在编译和运行,没有问题。
我真的很想找出原因并解决这个问题,以便其他开发人员无法推送带有打字稿错误的代码。 尽管文件是 .ts 或 .tsx,编译器或转译器的行为就好像打字稿不存在一样。
这是我的 tsconfig.json:
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"noFallthroughCasesInSwitch": true,
"module": "esnext",
"moduleResolution": "node",
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx",
"baseUrl": "src",
"noImplicitAny": true
},
"include": ["src", "src/**/*.ts", "src/**/*.tsx"],
}
这是我的 gatsby-config.ts 中的一个片段:
plugins: [
'gatsby-plugin-typescript',
我现在在运行时在终端中收到错误,但我确实收到了一些类型警告(尽管考虑到代码中 ts 错误的数量,我希望得到更多)。这是一个例子:
Warning: Failed prop type: The prop `to` is marked as required in `P`, but its value is `undefined`.
编辑: 我通过
yarn start
运行脚本,它从我的 package.json 的脚本块中获取:
"scripts": {
"develop": "dotenv-updater && gatsby develop",
"start": "dotenv-updater && gatsby develop",
"build": "gatsby build",
"serve": "gatsby serve",
"clean": "gatsby clean",
"typecheck": "tsc --noEmit",
"lint-staged": "lint-staged",
"lint": "eslint 'src/**/*.{js,ts,tsx,jsx}'",
"format": "prettier --write \"**/*.{js,jsx,tsx,ts,json,md}\"",
"prepare": "cd .. && husky"
},
更新
scripts
中的 package.json
以在 tsc
之前运行 gatsby
。
🗎
package.json
"scripts": {
"build": "tsc --noEmit && gatsby build",
"develop": "tsc --noEmit && gatsby develop",
"start": "tsc --noEmit && gatsby develop"
}
现在,如果您运行
yarn start
(或 yarn build
或 yarn develop
),TypeScript 编译器将首先处理您的代码。 --noEmit
标志告诉 tsc
处理 TypeScript 文件(检查错误等),但不产生任何 JavaScript 输出。然后,在 tsc
运行后,将执行所需的 gatsby
命令。
您可能还想在这些命令中的
dotenv-updater
之前或之后添加 tsc
。