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

TypeScript - Is there an option to disallow using "! in front of anything except a boolean?

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

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

在 TypeScript 中验证此 something 不是 undefinednull

这个非常容易出错!当用于 number 时,“0”将匹配,当用于 enum 时,第一项也将匹配(默认情况下,第一项的值为“0”)!

有没有办法在 TypeScript 中处理这个问题?有没有办法配置 TypeScript 以禁止在 boolean(和 any)以外的任何内容前面使用感叹号?这种配置是否有意义,还是我遗漏了一些微不足道的东西?

应该:

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

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

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

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

/*** 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.]

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

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

有关上述内容的更多详细信息,请参见here