Tslint: strict-type-predicates: ์ธ๋ฑ์Šค ์œ ํ˜•์˜ ๊ฑฐ์ง“ ๊ธ์ •

์— ๋งŒ๋“  2017๋…„ 06์›” 22์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: palantir/tslint

๋ฒ„๊ทธ ์‹ ๊ณ 

  • __TSLint ๋ฒ„์ „__: 5.4.3 5cf7100ad3c7b2fd3be62d94b7b4708460590a97
  • __TypeScript ๋ฒ„์ „__: 2.4.0
  • ____๋ฅผ ํ†ตํ•ด TSLint ์‹คํ–‰ํ•˜๊ธฐ: CLI

๊ด€๋ จ: https://github.com/Microsoft/TypeScript/issues/13778

๋ฆฐํŠธ๋˜๋Š” 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 } = {}

ajaff์— +1 - TypeScript์˜ ๋””์ž์ธ ํฌ์ธํŠธ์ด๋ฏ€๋กœ ๋‹ซ์Šต๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰