leona
leona

Reputation: 1

what' does the "heroes$!:Observable<Hero[]>;"mean?

what' does the "heroes$!:Observable<Hero[]>;"mean? And what' does the "heroes$?:Observable<Hero[]>;"mean?

Upvotes: 0

Views: 268

Answers (1)

Totati
Totati

Reputation: 1582

The ! is the manual type assertion operator to remove null and undefined from the type. You can read more about type assertions here.

The ? means it's optional, it can be undefined. It's a shortand for heroes$: Observable<Hero[]> | undefined. The documentation for mapping modifiers is here.

When you have strict settings enabled in your tslint.json, the compiler will complain about class properties that are not initialised where you declared then nor in the consrtuctor. For this you can do two things.

  1. Mark them with the optional ? modifier, or use an union type with undefined.
  2. Use ! to manually assert the type, and silence the compiler. With this you actually mean: "You can't know, but I know better this property will allways have a value". This is good when Angular sets the property. (like @Input(), @ViewChild(..., {static: true}))

Upvotes: 4

Related Questions