Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This source code has @Input properties that end with a !(此源代码具有以@Input结尾的@Input属性!)

.(。) Here's an example:(这是一个例子:) @Input() token!:StripeToken Why is it useful to have the !(为什么拥有!) in this case?(在这种情况下?) Some of the comments have noted that it is a non null assertion operator, but why is it useful (Or perhaps not useful) to have that in this particular scenario?(一些评论指出,它是一个非null的断言运算符,但是为什么在这种特殊情况下使用它是有用的(或可能没有用)?) I think the answer to this is that for Angular @Input properties having the non null assertion at the end of the property never makes sense but I wanted to see what the rest of you thought?(我认为这的答案是,对于在属性末尾具有非null断言的Angular @Input属性,从没有意义,但我想看看您其余部分的想法吗?) Update(更新资料) I tried it on a new Angular project and I get this error:(我在新的Angular项目上尝试了此操作,但出现此错误:) A definite assignment assertion '!'(确定分配断言“!”) is not permitted in this context.ts(1255)(ts(1255)在这种情况下是不允许的) So I don't think that it ever makes sense to inlude the !(因此,我认为包含!) operator on an @Input property.(@Input属性上的运算符。) Here's a screenshot:(这是屏幕截图:) 在此处输入图片说明   ask by Ole translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
322 views
Welcome To Ask or Share your Answers For Others

1 Answer

They use the compiler option strictPropertyInitialization so any class property not declared with type undefined and not initialized directly or in a constructor produces error TS2564.(他们使用编译器选项strictPropertyInitialization因此,未声明为undefined类型且未直接初始化或在构造函数中初始化的任何类属性都将产生错误TS2564。)

在此处输入图片说明 Using the definite assignment assertion modifier prevents this compiler error by telling TypeScript(使用确定分配断言修饰符可以通过告诉TypeScript防止此编译器错误) ... that a variable is indeed assigned for all intents and purposes, even if TypeScript's analyses cannot detect so.(……即使TypeScript的分析无法检测到变量,也确实为所有意图和目的分配了变量。) 在此处输入图片说明 Demo showing error in HelloComponent, add !(演示在HelloComponent中显示错误,请添加!) to get rid of the error(摆脱错误) Further reading: https://mariusschulz.com/blog/strict-property-initialization-in-typescript#solution-4-definite-assignment-assertion(进一步阅读: https : //mariusschulz.com/blog/strict-property-initialization-in-typescript#solution-4-definite-assignment-assertion)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

Just Browsing Browsing

[5] html - How to create even cell spacing within a

548k questions

547k answers

4 comments

86.3k users

...