1

I've seen somewhere written in the code fixture.componentInstance.dataSource!.data = [];

I want to know the meaning of dataSource!.data

I know about the meaning of question mark(?) before dot(.) like key1?.key2 but an Exclamation(!) before dot(.) !. expression makes me curious.

Many thanks in advance!!

Prshant Sharma
  • 79
  • 1
  • 3
  • 10
  • **"Non-null assertion operator"** It tells the compiler that `fixture.componentInstance.dataSource` is not null. It's a typescript 2.0 feature. https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#non-null-assertion-operator – Shashank Malviya Aug 04 '22 at 13:17

1 Answers1

2

It's called the Non-null Assertion Operator

https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#non-null-assertion-operator-postfix-

It removes null and undefined, allowing you to assert that you know there will be a value. Usually this is a code smell, something that should give you pause to consider other methods so that you can avoid using it. Type narrowing would be preferred. But in some specific instances, this operator can be a useful tool.

spex
  • 1,110
  • 10
  • 21