Tslint: strict-type-predicates:インデックスタイプでの誤検知

作成日 2017年06月22日  ·  3コメント  ·  ソース: palantir/tslint

バグレポート

  • __TSLintバージョン__:5.4.3 5cf7100ad3c7b2fd3be62d94b7b4708460590a97
  • __TypeScriptバージョン__:2.4.0
  • __TSLintの実行__:CLI

関連: https

リンティングされているTypeScriptコード

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
}

tslint.json構成の場合:

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

実際の/予想される動作

(Aは実際、Eは期待、Bは両方)

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

(実行例はここにあります:https://github.com/ypresto/tslint/tree/strict-type-predicates-index-type)

External

最も参考になるコメント

より簡単な例:

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

を生成します

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

ただし、エラーは発生しません。

全てのコメント3件

より簡単な例:

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

を生成します

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

ただし、エラーは発生しません。

tslintは、この種の特別な処理を追加するのに適切な場所ではありません。 これには、typescriptの型推論を変更する必要があります。 https://github.com/Microsoft/TypeScript/issues/13778

値を使用する前に常にundefinedをチェックする場合は、 undefinedを含むようにインデックス署名を定義することもできます。

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

+1 toajafff​​-これはTypeScriptの設計ポイントであるため終了します。

このページは役に立ちましたか?
0 / 5 - 0 評価