[英]Why does TypeScript not throw a compile error for an undefined type
Here's a simple repro to describe the problem:这是描述问题的简单再现:
npx react-native init MyApp --template react-native-template-typescript
我使用npx react-native init MyApp --template react-native-template-typescript
创建了一个 vanilla RN 项目App.tsx
, I added let foo: IThoughtTheWholePointOfATypeSafeLanguageWasNotToAllowShitLikeThis = "...seriously, wtf, why does this compile without an error!?";
在App.tsx
,我添加了let foo: IThoughtTheWholePointOfATypeSafeLanguageWasNotToAllowShitLikeThis = "...seriously, wtf, why does this compile without an error!?";
yarn iOS
我使用yarn iOS
运行该应用程序The app runs fine and there are no errors in the bundler.该应用程序运行良好,打包器中没有错误。
Question: How do I make it throw a compile error?问题:如何让它抛出编译错误?
You can use --strict
in your compiler options.您可以在编译器选项中使用--strict
。
This will:这会:
Enable all strict type checking options.启用所有严格的类型检查选项。 Enabling --strict enables --noImplicitAny, --noImplicitThis, --alwaysStrict, --strictBindCallApply, --strictNullChecks, --strictFunctionTypes and --strictPropertyInitialization.启用 --strict 启用 --noImplicitAny、--noImplicitThis、--alwaysStrict、--strictBindCallApply、--strictNullChecks、--strictFunctionTypes 和 --strictPropertyInitialization。
Source: https://www.typescriptlang.org/docs/handbook/compiler-options.html来源: https : //www.typescriptlang.org/docs/handbook/compiler-options.html
Figured it out.弄清楚了。
yarn tsc
does the compilation and generates type errors, if there are any. yarn tsc
进行编译并生成类型错误(如果有)。 You can chain this as part of other commands in your package.json
file, by preceding the existing command with yarn tsc &&
, if you need to fail on type errors before invoking the intended command.如果您需要在调用预期命令之前因类型错误而失败,您可以通过在现有命令前面加上yarn tsc &&
将其链接为package.json
文件中其他命令的一部分。
For example, one of the commands in my package.json
is as follows:比如我的package.json
中的命令之一如下:
"test": "yarn tsc && rm -f __tests__/**/*.js && jest --coverage"
The rm
bit may be helpful to you, as I've noticed that running tsc
generates .js
versions of all your .ts
files. rm
位可能对您有所帮助,因为我注意到运行tsc
生成所有.ts
文件的.js
版本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.