TypeScript - Is there an option to prohibit the use of "! Before anything other than a logical one?"

I understand that this may be a classic javascript issue, but I have too often found that I am using:

if (!something) { //... } 

to verify that something not undefined or null in TypeScript.

This is very error prone! When used in number "0" will match, and when used in enum first element will match too (by default, the first element is set to "0")!

Is there any way to handle this in TypeScript? Is there a way to configure TypeScript to forbid an exclamation mark before anything other than boolean (and any )? Will this configuration make sense or am I missing something trivial?

Must:

 if (something === null || something === undefined) { //... } 

instead, to verify that something is defined or not? And is there a way to ensure this in a team?

+7
javascript typescript tslint
source share
1 answer

You can use the strict-boolean-expressions TSLint rule to prohibit such things.

Here you can see some examples of the rule , but this is an excerpt that is especially relevant for your question. Places where the rule detects errors are marked with the ~~~ sign, and an error message is written next to this marking:

 /*** PrefixUnary Expressions ***/ /*** Invalid ***/ !!numType; ~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is a number. Only booleans are allowed.] !strType; ~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is a string. Only booleans are allowed.] !objType; ~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.] !enumType; ~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is an enum. Only booleans are allowed.] !!classType; ~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.] !bwrapType; ~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.] !!undefined; ~~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always truthy. Only booleans are allowed.] ~~~~~~~~~ [This type is not allowed in the operand for the '!' operator because it is always falsy. Only booleans are allowed.] /*** Valid ***/ !!boolFn(); !boolExpr; !!boolType; /*** If Statement ***/ /*** Invalid ***/ if (numType) { /* statements */ } ~~~~~~~ [This type is not allowed in the 'if' condition because it is a number. Only booleans are allowed.] if (objType) { /* statements */ } ~~~~~~~ [This type is not allowed in the 'if' condition because it is always truthy. Only booleans are allowed.] if (strType) { /* statements */ } ~~~~~~~ [This type is not allowed in the 'if' condition because it is a string. Only booleans are allowed.] if (bwrapType) { /* statements */ } ~~~~~~~~~ [This type is not allowed in the 'if' condition because it is always truthy. Only booleans are allowed.] if (strFn()) { /* statements */ } ~~~~~~~ [This type is not allowed in the 'if' condition because it is a string. Only booleans are allowed.] if (MyEnum.A) { /* statements */ } ~~~~~~~~ [This type is not allowed in the 'if' condition because it is an enum. Only booleans are allowed.] if (classType) { /* statements */ } ~~~~~~~~~ [This type is not allowed in the 'if' condition because it is always truthy. Only booleans are allowed.] 

To briefly answer another question, the code below is a great way to check if something is defined or not:

 if (something == null) { // will enter here if `something === null || something === undefined` } 

See here for more details.

+1
source share

All Articles