admin管理员组文章数量:1241125
This source code has @Input
properties that end with a !
. Here's an example:
@Input() token!:StripeToken
Why is it useful to have the !
in this case? Some of the ments 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?
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?
Update
I tried it on a new Angular project and I get this error:
A definite assignment assertion '!' is not permitted in this context.ts(1255)
So I don't think that it ever makes sense to inlude the !
operator on an @Input
property. Here's a screenshot:
This source code has @Input
properties that end with a !
. Here's an example:
@Input() token!:StripeToken
Why is it useful to have the !
in this case? Some of the ments 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?
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?
Update
I tried it on a new Angular project and I get this error:
A definite assignment assertion '!' is not permitted in this context.ts(1255)
So I don't think that it ever makes sense to inlude the !
operator on an @Input
property. Here's a screenshot:
- sry, wrong link stackoverflow./questions/42273853/… – Julius Dzidzevičius Commented Nov 28, 2019 at 17:35
- Yes - Much better - thanks! – Ole Commented Nov 28, 2019 at 17:36
- Not really sure why we would use that in an Angular context though? Why use this on an Angular @Input field? – Ole Commented Nov 28, 2019 at 17:37
- 2 typescriptlang/docs/handbook/release-notes/… – Alexander Staroselsky Commented Nov 28, 2019 at 17:38
-
In the handbook scenario it makes perfect sense, but I'm still not sure why they are adding it to the
@Input
field. The use cases within Angular always assume that these are not null and we don't get any pilation errors? – Ole Commented Nov 28, 2019 at 17:46
1 Answer
Reset to default 17They use the piler option strictPropertyInitialization
so any class property not declared with type undefined
and not initialized directly or in a constructor produces error TS2564.
To prevent this piler error they use the definite assignment assertion modifier which tells TypeScript
... that a variable is indeed assigned for all intents and purposes, even if TypeScript’s analyses cannot detect so.
Demo
Further reading: https://mariusschulz./blog/strict-property-initialization-in-typescript#solution-4-definite-assignment-assertion
Regarding your update
You didn't specify a type for the title
variable in your example, that's why you get error TS1255. Using !
in this context is possible and makes sense!
本文标签: javascriptAngular input property ending with exclamation pointStack Overflow
版权声明:本文标题:javascript - Angular @input property ending with exclamation point? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1739998163a2219447.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论