如何添加对打字稿类型系统的更多限制可以解决一些错误?

发布于 2025-02-04 02:13:02 字数 558 浏览 2 评论 0原文

我遇到了一些问题,因为库的类型定义有错误。 解决方案是添加strictnullchecks标志。

这对我来说真的是违反直觉的。添加限制如何消除错误?通常,这是相反的方式。

我可以找到我的错误在此github essupard earjears
另外,作为此用户指出

严格的项目不能在非严格的一个

中构建是没有意义的

,但是他们使用的是具有深通电类型的高级打字稿功能,并且没有记录他们的代码。

I had some issues building my TypeScript project because the type definitions of a library had errors.
The solution was to add the strictNullChecks flag.

That's really counter-intuitive to me. How does adding restrictions remove errors? Usually, it's the other way around.

The error that I had can be found on this GitHub issue.
Also, as this user pointed out:

it doesn't make sense that a strict project cannot build in a non strict one

I tried to understand their typings, but they are using advanced TypeScript features with deep generic types and they didn't documented their code.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云朵有点甜 2025-02-11 02:13:02

我认为,TypeScript实际上并未添加“检查”,它会修改nullUndefined的类型,这可能会导致类型错误(实际上,这就是它的本质) ,正如我所理解的。 https://www.typescriptlang.org/tsconfig/tsconfug#strictnullang-stsconf.strictnullchecks

当strictnullchecks为真时,无效和未定义的类型具有独特的类型,如果您尝试在预期混凝土值的情况下使用它们。

当您打开此选项时 - 它会以一种方式导致类型错误,并且当您修复所有错误并将选项返回时 - 它将以另一种方式引起错误。

I think, typescript does not actually add "checks", it modifies types of null and undefined, which may causes type errors (in fact, that's what it's meant to be), as I understood. https://www.typescriptlang.org/tsconfig#strictNullChecks ????????

When strictNullChecks is true, null and undefined have their own distinct types and you’ll get a type error if you try to use them where a concrete value is expected.

And when you turn this option on - it causes type errors in one way, and when you fix all errors and turn the option back - it will cause errors in another way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文