javascript - TypeScript - Postoji li opcija koja zabranjuje korištenje "!" ispred bilo čega osim boolean vrijednosti?


Razumijem da bi ovo mogao biti klasični problem s JavaScriptom, ali i ja sam se prečesto znao koristiti :

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

da se provjeri je li ovaj something nije undefined ili null, u TypeScriptu.

Ovo je vrlo sklono pogreškama! Kada se koristi na number, "0" će odgovarati, a kada se koristi na enum, odgovarat će i prvi element (po defaultu, prvi element ima vrijednost "0")!

Postoji li način da se s ovime nosimo u TypeScriptu? Postoji li način da se TypeScript konfigurira tako da zabranjuje uskličnik ispred bilo čega osim boolean (i any)? Bi li ova konfiguracija imala smisla ili mi nešto očito promiče?

Trebao bi :

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

može se koristiti umjesto toga, za provjeru je li nešto definirano ili nije? I postoji li način da se to provede u timu?

Odgovor #1

Možete koristiti TSLint pravilo strict-boolean-expressions kako biste zabranili ovakve stvari.

Ovdje možete vidjeti neke primjere pravila, ali ovo je odlomak koji je posebno relevantan za vaše pitanje. Mjesta gdje bi pravilo pronašlo pogreške označena su s ~~~, a poruka o pogrešci napisana je pored te oznake:

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

Da ukratko odgovorim na tvoje drugo pitanje, sljedeći isječak koda je dobar način da se provjeri je li nešto definirano ili nije:

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

Pogledajte ovdje za više detalja o gore navedenom

Povezani članci