TypeScript 2.0, introduces the non-nullable types - which means which should not be assigning null to variables unless we are explicit about it. Consider the following example:
Setting this value to the variable varNullOK to null means that we want to remove the existing value. Now, TypeScript will compile this. If we just declare a variable:
This new variable varNullOK2 is of type any and has the value undefined. This is because we have not initialised it. Again it is possible to assign null to this uninitialised variable.
We do this to reset a value in a variable. But, this can become a problem when we forget to refill this variable with an actual value. As, it can lead to miscalculations. Hence, this further leads to constant null checks in the code.
We can be explicit about which variable can be assigned a null value and which variable cannot be assigned a null value. For this, let us visit the tsconfig.json and set the variable strictNullChecks to true:
By default, this value is false. This check will ensure that we do not assign null to any variable accidentaly which should be holding only numbers.
After enabling strictNullChecks to true. If we compile the following:
It will give us an error. So, to remove the error, we can explicitly mention the variable to be a null by using the union type - or operator:
By doing this, we are telling the compiler that it is OK to have nulls in the variable.
Reference:
// nullable types
let varNullOK = 1234;
varNullOK = null;
Setting this value to the variable varNullOK to null means that we want to remove the existing value. Now, TypeScript will compile this. If we just declare a variable:
let varNullOK2;
This new variable varNullOK2 is of type any and has the value undefined. This is because we have not initialised it. Again it is possible to assign null to this uninitialised variable.
let varNullOK2;
varNullOK2 = null;
We do this to reset a value in a variable. But, this can become a problem when we forget to refill this variable with an actual value. As, it can lead to miscalculations. Hence, this further leads to constant null checks in the code.
We can be explicit about which variable can be assigned a null value and which variable cannot be assigned a null value. For this, let us visit the tsconfig.json and set the variable strictNullChecks to true:
"strictNullChecks": true, /* Enable strict null checks. */
By default, this value is false. This check will ensure that we do not assign null to any variable accidentaly which should be holding only numbers.
After enabling strictNullChecks to true. If we compile the following:
let varNullOK:number;
varNullOK = 1234;
varNullOK = null;
It will give us an error. So, to remove the error, we can explicitly mention the variable to be a null by using the union type - or operator:
let varNullOK:number|null;
varNullOK = 1234;
varNullOK = null;
By doing this, we are telling the compiler that it is OK to have nulls in the variable.
Reference:
No comments:
Post a Comment