在哪里设置 Angular 严格性标志,以配置 strictTemplates 的严格程度?

huangapple go评论75阅读模式
英文:

Where to set angular strictness flags that configure how strict strictTemplates is?

问题

我目前正在将我们的应用程序转换为strictTemplates,并且我们遇到了大量的错误,其中一些比其他的更相关。

所以我想配置angular类型检查器的严格性,并在文档中找到了这些严格性标志。

https://angular.io/guide/template-typecheck#troubleshooting-template-errors

以下是文档中的一些示例

strictInputTypes 检查绑定表达式对@Input()字段的可赋值性。还会影响指令泛型类型的推断。

strictInputAccessModifiers 在将绑定表达式分配给@Input()时,是否遵守private/protected/readonly等访问修饰符。如果禁用,将忽略@Input的访问修饰符;只检查类型。默认情况下,即使将strictTemplates设置为true,此选项也为false。

strictNullInputTypes 检查@Input()绑定时是否遵守strictNullChecks(根据strictInputTypes)。在使用未考虑到strictNullChecks的库时,关闭此选项可能会很有用。

[...]

我唯一无法回答的问题是在哪里设置它们... 所以应该在哪个文件中配置这些标志,以及如何配置?我在互联网上搜索了解决方案,但只找到了一大堆关于如何启用--strict以及其好处的文章。

英文:

I am currently converting our application to strictTemplates, and we are comming up with a huge amount of errors, some of them are more relevant than others.

So i wanted to configure the strictness of the angular type checker, and found this strictness flags in the documentation.

https://angular.io/guide/template-typecheck#troubleshooting-template-errors

Here are some examples from the docs

> strictInputTypes Whether the assignability of a binding expression to the @Input() field is checked. Also affects the inference of directive generic types.
>
> strictInputAccessModifiers Whether access modifiers such as private/protected/readonly are honored when assigning a binding expression to an @Input(). If disabled, the access modifiers of the @Input are ignored; only the type is checked. This option is false by default, even with strictTemplates set to true.
>
> strictNullInputTypes Whether strictNullChecks is honored when checking @Input() bindings (per strictInputTypes). Turning this off can be useful when using a library that was not built with strictNullChecks in mind.

[...]

The only question I could not answer was where to set them... So in which file should this flags configured, and how?

I searched the internet for a solution but did only find a huge swath of articles on how to enable --strict and what the benefits are.

答案1

得分: 1

Angular文档所示,您需要在tsconfig.json文件中的angularCompilerOptions字段中进行设置。

英文:

As shown in the Angular docs, you have to set them in the tsconfig.json file, inside the angularCompilerOptions field.

答案2

得分: 0

在这篇帖子中找到了解决方案: https://www.angular.love/en/2021/09/02/angular-compilation-restrictions-overview/

只需将以下标志放在tsconfig.app.json中的strictTemplates选项下:

...
"compilerOptions": {
  ...
  "strictNullChecks": true,
},
"angularCompilerOptions": {
  "strictTemplates": true,
  "strictInputTypes": true,
  "strictNullInputTypes": true
}
英文:

Found the solution in this post: https://www.angular.love/en/2021/09/02/angular-compilation-restrictions-overview/

Just put these flags below the strictTemplates option:

in tsconfig.app.json

   ...
     "compilerOptions": {
       ...
       "strictNullChecks": true,
     },
     "angularCompilerOptions": {
       "strictTemplates": true,
       "strictInputTypes": true,
       "strictNullInputTypes": true
     }
    }

huangapple
  • 本文由 发表于 2023年2月24日 14:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/75553161.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定