ํฐ์คํ ๋ฆฌ ๋ทฐ
๋ฌธ์
์ ์ ๊ฐ ์ ํํ ์์ดํ ์ ์ ๋ณด๋ฅผ ๋ด๊ณ ์๋ ๋ฆฌ์คํธ๋ฅผ ์ํํ์ฌ ํด๋น ์์ดํ ์ด ์กด์ฌํ๋์ง์ ์ฌ๋ถ๋ฅผ ํ์ธํ๊ธฐ ์ํด์ ๋ค์๊ณผ ๊ฐ์ด ๋ฌธ์์ด๋ก ๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ ๊ทผํ๋ ์ฝ๋๋ฅผ ์์ฑํ์๋ค.
infoList.forEach((info: InfoType) => {
let isSorted = false;
for (let i = 0; i < currentSelectedSortNum; i++) {
if (info[currentSortName].includes(currentSort[i])) {
isSorted = true;
break;
}
}
( ... )
});
ํ์ง๋ง ์ ๋๋ก ๊ธฐ๋ฅ์ด ๋์๋์ง ์์๊ณ ์ค๋ฅ๋ฅผ ํ์ธํ๊ธฐ ์ํด ์ฝ์๋ฅผ ๋ณด๋, ๋ค์๊ณผ ๊ฐ์ ๋ฉ์ธ์ง๊ฐ ๋ ์์๋ค.
Element implicitly has an 'any' type because type ( ... ) has no index signature.
๋ฌธ์ ํด๊ฒฐ
ํด๋น ๋ฌธ์ ๋ indexable ํ์ ๊ณผ ์๋ฐ์คํฌ๋ฆฝํธ ์์ธ์ ๋์๋ฐฉ์์ ๊ดํด ๋ชฐ๋ผ์ ์๊ธด ๋ฌธ์ ์๋ค.
์๋ฐ์คํฌ๋ฆฝํธ๋ ๊ฐ์ฒด์ ํ๋กํผํฐ์ ์ ๊ทผ์ ํ ๋ ๋ฌธ์์ด๋ก ์ ๊ทผํ ์ ์์ผ๋ฉฐ ์ด๋ ์๋ฐ์คํฌ๋ฆฝํธ ์์ธ์ ๋์๋ฐฉ์์ ์ํด ๊ฐ์ฒด์ ์์ธ์ ์ ๊ทผํ ๋ ๋ด๋ถ์ ์ผ๋ก toString() ๋ฉ์๋๋ฅผ ํธ์ถํ์ฌ ๋ฌธ์์ด๋ก ๋ณํ๋ ๊ฐ์ ํตํด ์ ๊ทผํ๋ค.
// ES6
let obj = {
toString() {
console.log('toString() called');
}
};
let foo = {};
foo[obj] = 'Key is obj'; // toString() called
console.log(foo[obj]);
// toString() called
// Key is obj
ํ์ง๋ง ๋ค์๊ณผ ๊ฐ์ ์ฝ๋๋ฅผ ๋๊ฐ์ด ํ์ ์คํฌ๋ฆฝํธ์์ ์ฌ์ฉํ๊ฒ ๋๋ค๋ฉด, ์์ ๊ฐ์ ์๋ฌ ๋ฉ์ธ์ง๊ฐ ๋์จ๋ค.
์ด์ ์ธ ์ฆ์จ, ํ๋กํผํฐ์ ์ ๊ทผํ ๋ ์ด๋ค ํ์ ์ธ์ง ํ์ธํ ์ ์์ด ์๋ฌต์ ์ผ๋ก any ํ์ ์ ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ด๋ค.
์ด๋ tsconfig์ "noImplicitAny": true ์ด๊ธฐ ๋๋ฌธ์ ๋ฐ์ํ๋ ์๋ฌ์ด๋ค.
ํด๋น ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด์ ์ธ๋ฑ์ค ์๊ทธ๋์ณ(index signature)๋ฅผ ์ฌ์ฉํ๋ฉด ๋์๋ค.
(์๋ ์ธํฐํ์ด์ค ์ ์ธ ๋ ์ธ๋ฑ์ค ์๊ทธ๋์ณ๋ 'key ๊ฐ์ string์ด๊ณ ๋ฐํ๊ฐ์ any' ์ด๋ผ๋ ์๋ฏธ๋ฅผ ๋ด๊ณ ์๋ค)
export interface InfoType {
[key: string]: any;
id: number;
title: string;
client: string;
due: string;
count: number;
amount: number;
method: string[];
material: string[];
status: string;
}
Reference
https://heecheolman.tistory.com/64#%EC%A4%91%EC%B2%A9%EB%90%9C-index-signature
[typescript] ํ์ ์คํฌ๋ฆฝํธ ์ธํฐํ์ด์ค
typescript03 ํ์ ์คํฌ๋ฆฝํธ ์ธํฐํ์ด์ค ๊ธฐ์กด์ ์๋ฐ์คํฌ๋ฆฝํธ์๋ ์ธํฐํ์ด์ค๋ผ๋ ๊ฐ๋ ์ด ์์์ต๋๋ค. ํ์ง๋ง ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ด์ฉํด ์ธํฐํ์ด์ค๋ฅผ ์ฌ์ฉํ ์ ์๊ฒ ๋์ต๋๋ค. ์ธํฐํ์ด์ค๋ผ๋ ์ฉ์ด
heecheolman.tistory.com