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

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

问题描述

我最近开始在 Expo 中使用 TypeScript.我已经完成了所有 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-plugin-transform-typescript

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.

更新:我发现了另一个实例,最近在将 jesttypescript 添加到项目时也适用.在 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#使用打字稿

但是,将 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天全站免登陆