这可以通过启用来解决noFallthroughCasesInSwitch
选项在你的tsconfig.json
。查看讨论here https://github.com/facebook/create-react-app/issues/9429#issuecomment-674994709了解更多信息。
{
"compilerOptions": {
"noFallthroughCasesInSwitch": true,
...
},
...
}
对于任何好奇的人来说,上述解决方案并没有修复该错误。它只是跳过运行下面的错误代码,如果未提供,该代码会将建议值分配给打字稿编译器选项。这tsconfig.json
产生于react-scripts
默认情况下没有noFallthroughCasesInSwitch
选项。添加该选项无需运行代码。
// Some options when not present in the tsconfig.json will be assigned
// a suggested value which crashes the program
if (suggested != null) {
if (parsedCompilerOptions[option] === undefined) {
appTsConfig.compilerOptions[option] = suggested; // error here
...
}
}
EDIT:
如果脚本因其他选项而崩溃,并且您的堆栈跟踪与我的问题中的堆栈跟踪相同,则您应该检查您的代码中是否缺少以下编译器选项tsconfig.json
These https://github.com/facebook/create-react-app/blob/ed958938f642007645dd5ac3466db36202f8754e/packages/react-scripts/scripts/utils/verifyTypeScriptSetup.js#L116-L160是 Typescript 编译器选项中未指定时的建议值tsconfig.json
const compilerOptions = {
// These are suggested values and will be set when not present in the
// tsconfig.json
target: {
parsedValue: ts.ScriptTarget.ES5,
suggested: 'es5',
},
lib: { suggested: ['dom', 'dom.iterable', 'esnext'] },
allowJs: { suggested: true },
skipLibCheck: { suggested: true },
esModuleInterop: { suggested: true },
allowSyntheticDefaultImports: { suggested: true },
strict: { suggested: true },
forceConsistentCasingInFileNames: { suggested: true },
noFallthroughCasesInSwitch: { suggested: true },
module: {
parsedValue: ts.ModuleKind.ESNext,
value: 'esnext',
reason: 'for import() and import/export',
},
moduleResolution: {
parsedValue: ts.ModuleResolutionKind.NodeJs,
value: 'node',
reason: 'to match webpack resolution',
},
resolveJsonModule: { value: true, reason: 'to match webpack loader' },
isolatedModules: { value: true, reason: 'implementation limitation' },
noEmit: { value: true },
jsx: {
parsedValue: ts.JsxEmit.React,
suggested: 'react',
},
paths: { value: undefined, reason: 'aliased imports are not supported' },
};
您需要将这些选项显式添加到您的tsconfig.json
因此脚本可以跳过有问题的分支并避免崩溃。