TypeScript - 是否有一个选项可以禁止在除布尔值之外的任何内容之前使用"!

ele*_*ype 9 javascript typescript tslint

我知道这可能是一个经典的javascript问题,但我经常发现自己使用:

if (!something) {
    //...
}
Run Code Online (Sandbox Code Playgroud)

在TypeScript中验证这something不是undefinednull.

这非常容易出错!当用在number"0"上时会匹配,当在enum第一个项目上使用时也会匹配(默认情况下,第一个项目的值为"0")!

有没有办法在TypeScript中处理这个问题?有没有办法配置TypeScript以禁止除boolean(和any)之外的任何内容的感叹号?这种配置是否有意义,或者我错过了一些微不足道的东西?

应该 :

if (something === null || something === undefined) {
    //...
}
Run Code Online (Sandbox Code Playgroud)

相反,用来验证是否定义了某些东西?有没有办法在团队中强制执行此操作?

JKi*_*ian 2

您可以使用strict-boolean-expressions TSLint 规则来禁止此类事情。

您可以在此处查看该规则的一些示例,但这是与您的问题特别相关的摘录。规则会发现错误的地方标记为~~~,错误消息写在该标记旁边:

/*** 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.]
Run Code Online (Sandbox Code Playgroud)

为了简要回答您的另一边问题,下面的代码片段是检查某些内容是否已定义的好方法:

if (something == null) {
    // will enter here if `something === null || something === undefined`
}
Run Code Online (Sandbox Code Playgroud)

有关上述内容的更多详细信息,请参阅此处