Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the exclamation mark stands for on Vue Property Decorator [duplicate]

I am getting familiar with the use of typescript in vue. When reading the documentation of vue property decorator I cannot get what this declaration stands for

@Prop({ default: 'default value' }) readonly propB!: string
like image 857
Petran Avatar asked Dec 29 '19 14:12

Petran


1 Answers

In this specific case, the bang operator is used because you know that your property propB cannot be null or undefined as the decorator takes care of filling in a value. TypeScript does not know this and since you are not assigning a value directly or in the constructor, it expects the type signature to be string | undefined. The exlamation mark tells TypeScript that you know the value will never be undefined and reduces the signature to string only without complaining about the possibility of it being undefined.

You only need this if you set the TypeScript compiler option to strict (this is strongly encouraged!).

like image 107
pascalpuetz Avatar answered Oct 13 '22 02:10

pascalpuetz