简体   繁体   English

为什么 TypeScript 不会为未定义的类型抛出编译错误

[英]Why does TypeScript not throw a compile error for an undefined type

Here's a simple repro to describe the problem:这是描述问题的简单再现:

  1. I created a vanilla RN project using npx react-native init MyApp --template react-native-template-typescript我使用npx react-native init MyApp --template react-native-template-typescript创建了一个 vanilla RN 项目
  2. In 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!?";
  3. I run the app using 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.

相关问题 为什么 Typescript 会抛出错误信息:类型上不存在属性? - Why Typescript throw error message: Property does not exist on type? 当 function 参数类型不匹配时,为什么 TypeScript 不抛出错误? - Why does TypeScript not throw an error when the function argument type is mismatched? 为什么jquery.d.ts TypeScript定义文件会抛出编译错误? - Why does the jquery.d.ts TypeScript definition file throw a compile error? Typescript union 类型不抛出错误 - Typescript union type does not throw error Typescript参数类型不匹配不会抛出错误 - Typescript argument type mismatch does not throw error 为什么 TypeScript 会抛出错误:TS2339:“EventTarget”类型上不存在属性“错误” - Why does TypeScript throw the error: TS2339: Property 'error' does not exist on type 'EventTarget' 打字稿声明:为什么这会引发错误 - Typescript declaration: why does this throw an error 为什么 typescript 会在一个数字上抛出错误 | 未定义的比较,即使在使用可选链接之后? - Why does typescript throw an error on an number | undefined comparison, even after using optional chaining? 为什么 TypeScript 在检查联合类型的 if 语句中会抛出错误,就好像它只是文字类型一样? - Why does TypeScript throw an error in an if statement checking an Union Type as if it's just a Literal Type? 泛型打字稿,为什么这段代码会抛出错误 Type '{}' cannot be assigned to a type? - Generics typescript, why does this code throw an error Type '{}' cannot be assigned to a type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM