尽管TypeScript编译器出错,为什么我的React Native应用仍能成功构建? [英] Why does my React Native app build successfully despite TypeScript compiler error?

查看:56
本文介绍了尽管TypeScript编译器出错,为什么我的React Native应用仍能成功构建?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始将TypeScript与Expo一起使用.我已经完成了所有的linter/formatter集成,例如 typescript-eslint ,因此我可以在编码过程中捕获大多数错误.为了检查代码是否可以编译,我不时地运行 npx tsc 并进行相应的修复.

I've recently started using TypeScript with Expo. I've done all the linter/formatter integrations like typescript-eslint so I can catch most of the errors during coding. To check if the code compiles, I run npx tsc every once in a while and fix accordingly.

我还没有完全掌握的一件事是,即使有许多编译错误,我的应用程序仍能成功构建的原因.我希望(并希望)针对每个编译错误都看到一个红色屏幕错误,而不是应用程序构建成功,然后我找到了它.例如,

One thing that I haven't fully grasped yet is why my app builds successfully even when there are numerous compile errors. I expect (and prefer) to see a red screen error for every compile error rather than the app build successfully and me find it out later. For example,

function square<T>(x: T): T {
  console.log(x.length); // error TS2339: Property 'length' does not exist on type 'T'.
  return x * x;
}

是典型的TypeScript错误(我相信吗?),可以在编译时轻松检查.我希望它导致大的红色屏幕错误,并且构建失败.

is a typical TypeScript error that (I believe?) can be easily checked at compile time. I want it to result in a big red screen error and the build to fail.

我对TypeScript还是很陌生,所以有可能我错过了一些非常重要的内容.究竟是什么原因导致这种宽大处理,有没有办法实施更严格的检查?

I'm quite new to TypeScript so it's possible that I'm missing something very important. What exactly is causing this leniency and is there a way to enforce stricter checks?

推荐答案

首先要了解的是Typescript是Javascript的超集,在这种情况下,实际上在编译过程中不会进行类型检查.

The first thing to understand is that Typescript is a superset of Javascript, and in this case it doesn't actually get type checked during compilation.

从本质上讲,Babel只是剥离了Typescript并将其转换为Javascript,然后将其编译为js包.

Essentially what happens is Babel just strips out the Typescript and converts it to Javascript, which then gets compiled into the js bundles.

您可以查看以下Babel文档的第一行以及注意事项: https://babeljs.io/docs/en/next/babel-插件转换类型脚本

You can take a look at the first line of the following Babel docs as well as the caveats: https://babeljs.io/docs/en/next/babel-plugin-transform-typescript

由于Babel不进行类型检查,因此语法上正确但会失败的代码可能无法成功地转换为TypeScript类型检查,并且常常以意外或无效的方式进行转换.

Since Babel does not type-check, code which is syntactically correct, but would fail the TypeScript type-checking may successfully get transformed, and often in unexpected or invalid ways.

我建议将您的构建命令扩展为首先包含 tsc 或更确切地说是Typescript编译,并且在tsconfig中将 noEmit 设置为true.

What I would suggest is extending your build command to first include tsc or rather Typescript compilation, with noEmit set to true in your tsconfig.

更新:我最近发现了另一个实例,该实例在将 jest typescript 添加到项目中时适用.在Jest文档的底部,他们实际上声明了同一件事:

Update: I found another instance where this applies recently when adding jest and typescript to a project. At the bottom of the Jest docs they actually state the same thing:

https://jestjs.io/docs/en/getting-started#using-typescript

但是,将TypeScript与Babel结合使用时有一些注意事项.由于Babel中对TypeScript的支持是编译,因此Jest在运行测试时不会进行类型检查.如果需要,可以使用ts-jest.

However, there are some caveats to using TypeScript with Babel. Because TypeScript support in Babel is transpilation, Jest will not type-check your tests as they are run. If you want that, you can use ts-jest.

这篇关于尽管TypeScript编译器出错,为什么我的React Native应用仍能成功构建?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆