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:

Share Improve this question edited Nov 28, 2019 at 21:43 Ole asked Nov 28, 2019 at 17:32 OleOle 47k69 gold badges237 silver badges442 bronze badges 8
  • 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
 |  Show 3 more ments

1 Answer 1

Reset to default 17

They 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