Tslint: strict-type-predicates : faux positif avec les types d'index

Créé le 22 juin 2017  ·  3Commentaires  ·  Source: palantir/tslint

Rapport d'erreur

  • __Version TSLint__ : 5.4.3 5cf7100ad3c7b2fd3be62d94b7b4708460590a97
  • __TypeScript version__ : 2.4.0
  • __Exécution de TSLint via__ : CLI

Lié à : https://github.com/Microsoft/TypeScript/issues/13778

Code TypeScript en train d'être lint

declare function get<T>(): T;
// index types
{
    get<{ [key: string]: string }>()['foo'] === undefined
    get<{ [key: string]: string }>()['foo'] === null
    get<{ [key: string]: string }>()['foo'] == undefined
    get<{ [key: string]: string }>()['foo'] == null

    get<{ [index: number]: string }>()[0] === undefined
    get<{ [index: number]: string }>()[0] === null
    get<{ [index: number]: string }>()[0] == undefined
    get<{ [index: number]: string }>()[0] == null
}

avec la configuration tslint.json :

(En utilisant test/rules/strict-type-predicates/strict-null-checks/tsconfig.json )

Comportement réel / attendu

(A est réel, E est attendu, B est les deux)

{
    get<{ [key: string]: string }>()['foo'] === undefined
A:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
    get<{ [key: string]: string }>()['foo'] === null
B:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
    get<{ [key: string]: string }>()['foo'] == undefined
A:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
E:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Use '=== undefined' instead.]
    get<{ [key: string]: string }>()['foo'] == null
A:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
E:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Use '=== undefined' instead.]

    get<{ [index: number]: string }>()[0] === undefined
A:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
    get<{ [index: number]: string }>()[0] === null
B:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
    get<{ [index: number]: string }>()[0] == undefined
A:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
E:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Use '=== undefined' instead.]
    get<{ [index: number]: string }>()[0] == null
A:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Expression is always false.]
E:  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ [Use '=== undefined' instead.]
}

(L'exemple d'exécution est ici : https://github.com/ypresto/tslint/tree/strict-type-predicates-index-type )

External

Commentaire le plus utile

Exemple plus simple :

const foo: { [key: string]: string } = {}
if (foo.bar === undefined) {
    // blah
}

produira

ERROR: /Users/yuya/repo/github.com-private/codetakt/pf-user-admin/client/test.ts[2, 5]: Expression is always false.

mais aucune erreur n'est attendue.

Tous les 3 commentaires

Exemple plus simple :

const foo: { [key: string]: string } = {}
if (foo.bar === undefined) {
    // blah
}

produira

ERROR: /Users/yuya/repo/github.com-private/codetakt/pf-user-admin/client/test.ts[2, 5]: Expression is always false.

mais aucune erreur n'est attendue.

tslint n'est pas le bon endroit pour ajouter ce genre de traitement spécial. Cela nécessiterait un changement dans l'inférence de type dactylographié. https://github.com/Microsoft/TypeScript/issues/13778

Si vous vérifiez toujours undefined avant d'utiliser la valeur, vous pouvez également définir votre signature d'index pour inclure undefined ;

const foo: { [key: string]: string | undefined } = {}

+1 à ajafff ​​- fermeture car il s'agit d'un point de conception de TypeScript.

Cette page vous a été utile?
0 / 5 - 0 notes

Questions connexes

dashmug picture dashmug  ·  3Commentaires

cateyes99 picture cateyes99  ·  3Commentaires

mrand01 picture mrand01  ·  3Commentaires

CSchulz picture CSchulz  ·  3Commentaires

zewa666 picture zewa666  ·  3Commentaires