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

Posted

技术标签:

【中文标题】TypeScript - 是不是有一个选项可以禁止在除布尔值之外的任何内容前面使用“!【英文标题】:TypeScript - Is there an option to disallow using "! in front of anything except a boolean?TypeScript - 是否有一个选项可以禁止在除布尔值之外的任何内容前面使用“! 【发布时间】:2017-08-24 13:59:36 【问题描述】:

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

if (!something) 
    //...

在 TypeScript 中验证此 something 不是 undefinednull

这很容易出错!在number 上使用时,“0”将匹配,而在enum 上使用时,第一项也将匹配(默认情况下,第一项的值为“0”)!

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

应该:

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

改为用于验证是否已定义某些内容?有没有办法在团队中强制执行?

【问题讨论】:

出于某种原因,“经典 javascript”让我不寒而栗。就个人而言,我认为打字稿更“经典”。 你也许可以为它写一个 tslint 规则。 您可以编写自定义 lint 规则来防止这种情况。除非您将其设置为观看,否则在您运行 linting 任务之前它不会捕获它。至少你会得到通知它已经发生了,然后你可以去修复它。如果发现任何错误,我们将 linting 任务设置为使构建失败。这样,无论谁编写它,都必须在合并之前对其进行修复。 palantir.github.io/tslint/usage/custom-rules 【参考方案1】:

您可以使用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

【讨论】:

这显然是答案,谢谢。遗憾的是,我希望这条规则在 VSCode 中正常工作,但由于它需要类型检查,它似乎只能手动运行(github.com/Microsoft/vscode-tslint/issues/70)。但这是另一个故事!

以上是关于TypeScript - 是不是有一个选项可以禁止在除布尔值之外的任何内容前面使用“!的主要内容,如果未能解决你的问题,请参考以下文章

Typescript

Angular Material + TypeScript:自动完成 + 选择选项

Typescript - 如何禁止两个解析为相同类型的类型别名互换使用?

在 TypeScript 中禁止可索引类型

sql select查询中是不是禁止使用逗号?

如何禁止 alert在浏览器中弹出信息。