Currently I stumbled upon the ContentChildren decorator of Angular. In the first code example the following syntax is used:
import {AfterContentInit, ContentChildren, Directive, QueryList} from '@angular/core';
@Directive({selector: 'child-directive'})
class ChildDirective {
}
@Directive({selector: 'someDir'})
class SomeDir implements AfterContentInit {
@ContentChildren(ChildDirective) contentChildren !: QueryList<ChildDirective>; // this line is relevant
ngAfterContentInit() {
// contentChildren is set
}
}
Note the exclamation mark followed by a colon right after the @ContentChildren(ChildDirective) contentChildren
variable definition. In this StackOverflow thread I discovered that this syntax can be used as a "non-null assertion operator" when accessing a variable or object property.
My question is now whether the exclamation mark before a type definition has exactly the same meaning like in a normal context. Does it simply say the TypeScript compiler: "Okay, don't worry about null
or undefined", or does this syntax have another specific meaning in this context?
The exclamation mark, !, also sometimes referred to as the exclamation point, especially in American English, is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings, or to show emphasis. The exclamation mark often marks the end of a sentence, for example: "Watch out!"
The exclamation mark ! is known as the non-null assertion operator in TypeScript. We will be using these terms interchangeably in this article. But what does this operator do? What is the TypeScript exclamation mark? What is the TypeScript exclamation mark?
Like a period or question mark, an exclamation point typically comes at the end of a sentence. It is most often seen in informal text. Exclamation marks make the greatest impact when they are used sparingly. Follow these easy-to-remember rules when you use them.
Rules for Using Exclamation Marks 1 Use an exclamation mark at the end of a strong command, an interjection, or an emphatic declaration. ... 2 Exclamation points may be used to convey extreme emotion at the end of a question. ... 3 Surround an exclamation mark with parentheses to emphasize a single word in a sentence. ... More items...
My question is now whether the exclamation mark before a type definition has exactly the same meaning like in a normal context.
No that's actually not the same thing, in this context it does something different. Normally when you declare a member (which doesnt' include undefined
in it's type) it has to be initialized directly or in the constructor. If you add !
after the name, TypeScript will ignore this and not show an error if you don't immediately initialize it:
class Foo {
foo: string; // error: Property 'foo' has no initializer and is not definitely assigned in the constructor.
bar!: string; // no error
}
The same thing actually applies to local variables as well:
let foo: string;
let bar!: string;
console.log(foo); // error: Variable 'foo' is used before being assigned.
console.log(bar); // no error
Playground
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With