While perusing through my organization's source repository I came across this little gem:
RawParameterStorage[!ParameterWorkingIdx][ParameterDataOffset] = ...
Is this valid code? (It compiles) What does the exclamation mark here do?
An invert ~
operator might make sense, since it's commonly confused with the not !
operator in boolean expressions. However, it doesn't seem to make logical sense to impose the not !
operator on an array index. Any Thoughts?
What is the TypeScript exclamation mark? The non-null assertion operator tells the TypeScript compiler that a value typed as optional cannot be null or undefined . For example, if we define a variable as possibly a string or undefined, the !
In Javascript, the exclamation mark (“!”) symbol, called a “bang,” is the logical “not” operator. Placed in front of a boolean value it will reverse the value, returning the opposite. ! true; // Returns false.
The exclamation mark (non-null assertion) operator removes null and undefined from the type of an expression. It is used when we we know that a variable that TypeScript thinks could be null or undefined actually isn't. Copied!
In programming and scripting languages, the exclamation mark is used as "not." For example, "! =" also means not equal. See our operator definition for further information. Used to help identify a nonexecutable statement.
!ParameterWorkingIdx
Means ParameterWorkingIdx
is 0
, If it is, !ParameterWorkingIdx
evaluates as true
which might be implicitly converted to the indexer type (For example, 1
for integer indexer as in an array), otherwise, it evaluates as false
.
If ParameterWorkingIdx == 0
then [!ParameterWorkingIdx] == [1]
.
If ParameterWorkingIdx != 0
then [!ParameterWorkingIdx] == [0]
.
It also depends on other stuff like:
The type of ParameterWorkingIdx
.
overloading of ! operator
by the type of ParameterWorkingIdx
.
indexer overloading by the type of RawParameterStorage
.
etc...
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