๊ด๋ จ: https://github.com/Microsoft/TypeScript/issues/13778
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 )
๋ ๊ฐ๋จํ ์:
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์ ๋์์ธ ํฌ์ธํธ์ด๋ฏ๋ก ๋ซ์ต๋๋ค.
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
๋ ๊ฐ๋จํ ์:
์์ฐํ ๊ฒ์ด๋ค
๊ทธ๋ฌ๋ ์ค๋ฅ๊ฐ ์์๋์ง ์์ต๋๋ค.