Tslint: Strict-Type-Prädikate: Falsch positiv bei Indextypen

Erstellt am 22. Juni 2017  ·  3Kommentare  ·  Quelle: palantir/tslint

Fehlerbericht

  • __TSLint-Version__: 5.4.3 5cf7100ad3c7b2fd3be62d94b7b4708460590a97
  • __TypeScript-Version__: 2.4.0
  • __TSLint ausführen über__: CLI

Bezogen auf: https://github.com/Microsoft/TypeScript/issues/13778

TypeScript-Code wird linted

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
}

mit tslint.json Konfiguration:

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

Tatsächliches / erwartetes Verhalten

(A ist tatsächlich, E ist erwartet, B ist beides)

{
    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.]
}

(Laufendes Beispiel ist hier: https://github.com/ypresto/tslint/tree/strict-type-predicates-index-type )

External

Hilfreichster Kommentar

Einfacheres Beispiel:

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

wird herstellen

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

aber es wird kein Fehler erwartet.

Alle 3 Kommentare

Einfacheres Beispiel:

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

wird herstellen

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

aber es wird kein Fehler erwartet.

tslint ist nicht der richtige Ort, um diese Art von Sonderbehandlung hinzuzufügen. Dies würde eine Änderung des Typrückschlusses des Typoskripts erfordern. https://github.com/Microsoft/TypeScript/issues/13778

Wenn Sie immer nach undefined suchen, bevor Sie den Wert verwenden, können Sie Ihre Indexsignatur auch so definieren, dass sie undefined ;

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

+1 bis ajafff ​​- wird geschlossen, da dies ein Entwurfspunkt von TypeScript ist.

War diese Seite hilfreich?
0 / 5 - 0 Bewertungen