
๐ Effective TypeScript ์ฑ ์ ์ฝ๊ณ ๊ณต๋ถํ ๋ด์ฉ์ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
ํ์ ์คํฌ๋ฆฝํธ์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๊ด๊ณ
- ํ์ ์คํฌ๋ฆฝํธ๋ ๋ฌธ๋ฒ์ ์ผ๋ก๋ ์๋ฐ์คํฌ๋ฆฝํธ์ ์์์งํฉ
- ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ์ ๋ฌธ๋ฒ ์ค๋ฅ๊ฐ ์๋ค๋ฉด
- ์ ํจํ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ์ด๋ผ๊ณ ํ ์ ์๋ค.
๋ฌธ๋ฒ์ ์ ํจ์ฑ๊ณผ ๋์์ ์ด์๋ ๋ ๋ฆฝ์ ์ธ ๋ฌธ์
- ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ์ ์ด๋ค ์ด์๊ฐ ์กด์ฌํ๋ค๋ฉด ๋ฌธ๋ฒ ์ค๋ฅ๊ฐ ์๋๋๋ผ๋ ํ์ ์ฒด์ปค์๊ฒ ์ง์ ๊ฐ๋ฅ์ฑ ๋์
- ํ์ง๋ง, ํ์ ์คํฌ๋ฆฝํธ๋ ์ฌ์ ํ ์์ฑ๋ ์ฝ๋๋ฅผ ํ์ฑํ๊ณ ์๋ฐ์คํฌ๋ฆฝํธ๋ก ๋ณํ ๊ฐ๋ฅ
ํ์ฅ์์ ์ฐจ์ด
- ์๋ฐ์คํฌ๋ฆฝํธ ํ์ผ์ .js ๋๋ .jsx ํ์ฅ์
- ํ์ ์คํฌ๋ฆฝํธ ํ์ผ์ .ts ๋๋ .tsx ํ์ฅ์
- ํ์ง๋ง ํ์
์คํฌ๋ฆฝํธ๋ ์์์งํฉ์ด๊ธฐ ๋๋ฌธ์ main.js๋ฅผ main.ts๋ก ๋ฐ๊พผ๋ค๊ณ ํด๋ ๋ฌ๋ผ์ง๋ ๊ฒ์ ์๋ค.
- ๋ฐ๋ผ์ ๋ง์ด๊ทธ๋ ์ด์ ํ๋ ๋ฐ ์์ฒญ๋ ์ด์
- ๊ธฐ์กด ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์ ์งํ๋ฉด์ ์ผ๋ถ๋ถ์๋ง ํ์ ์คํฌ๋ฆฝํธ ์ ์ฉ์ด ๊ฐ๋ฅํ๊ธฐ ๋๋ฌธ
๋ชจ๋ ์๋ฐ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ์ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ
- ์ ๋ช
์ ๋ ์ฐธ์ด์ง๋ง, ๋ฐ๋๋ ์ฑ๋ฆฝํ์ง X
- ํ์ ์คํฌ๋ฆฝํธ๊ฐ ํ์ ์ ๋ช ์ํ๋ ์ถ๊ฐ์ ์ธ ๋ฌธ๋ฒ์ ๊ฐ์ง๊ธฐ ๋๋ฌธ
์๋ ์ฝ๋๋ ์ ํจํ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ
function greet(who: string) {
console.log('Hello', who);
}
TypeError๋ฅผ ๋ฐ์์ํค๋ ์๋ฐ์คํฌ๋ฆฝํธ
let city = 'new york city';
console.log(city.toUppercase());
ํ์ง๋ง, ํ์ ์คํฌ๋ฆฝํธ์ ํ์ ์ฒด์ปค๋ ๋ฌธ์ ๋ฅผ ์ฐพ์๋ธ๋ค.
- toUppercase ์์ฑ์ด 'string' ํ์์ ์์ต๋๋ค.
let city = 'new york city';
console.log(city.toUppercase());
// ~~~~~~~~~~~ Property 'toUppercase' does not exist on type
// 'string'. Did you mean 'toUpperCase'?
์ฆ, ํ์ ์คํฌ๋ฆฝํธ๋ ์ด๊น๊ฐ์ผ๋ก๋ถํฐ ํ์ ์ ์ถ๋ก ํ๋ค.
ํ์ ์์คํ ์ ๋ชฉํ
ํ์ ์์คํ ์ ๋ชฉํ ์ค ํ๋๋, ๋ฐํ์์ ์ค๋ฅ๋ฅผ ๋ฐ์์ํฌ ์ฝ๋๋ฅผ ๋ฏธ๋ฆฌ ์ฐพ์๋ด๋ ๊ฒ
- ํ์ ์คํฌ๋ฆฝํธ๊ฐ '์ ์ ' ํ์ ์์คํ ์ด๋ผ๋ ๊ฒ๋ ํ๋์ ํน์ง
์ค๋ฅ๊ฐ ๋ฐ์ํ์ง ์์ง๋ง ์๋์ ๋ค๋ฅด๊ฒ ๋์ํ๋ ์ฝ๋
const states = [
{name: 'Alabama', capital: 'Montgomery'},
{name: 'Alaska', capital: 'Juneau'},
{name: 'Arizona', capital: 'Phoenix'},
// ...
];
for (const state of states) {
console.log(state.capitol);
}
์คํ๊ฒฐ๊ณผ
undefined
undefined
undefined
- ์ ํจํ ์๋ฐ์คํฌ๋ฆฝํธ์ด๋ฉฐ ์ค๋ฅ ์์ด ์คํ๋๋ค.
- ํ์ง๋ง, ๋ฃจํ ๋ด์ state.capitol ์ ์๋ํ ์ฝ๋๊ฐ ์๋
- ํ์ ์คํฌ๋ฆฝํธ ํ์ ์ฒด์ปค๋ ์ถ๊ฐ์ ์ธ ํ์ ๊ตฌ๋ฌธ์ด ์์ด๋ ์ค๋ฅ๋ฅผ ์ฐพ์๋ธ๋ค.
// HIDE
export const foo = true;
const states = [
{name: 'Alabama', capital: 'Montgomery'},
{name: 'Alaska', capital: 'Juneau'},
{name: 'Arizona', capital: 'Phoenix'},
// ...
];
// ENDfor (const state of states) {
console.log(state.capitol);
// ~~~~~~~ Property 'capitol' does not exist on type// '{ name: string; capital: string; }'.// Did you mean 'capital'?
}
- ์ด์ฒ๋ผ ํ์ ๊ตฌ๋ฌธ ์์ด๋ ์ค๋ฅ๋ฅผ ์ก์ ์ ์๋ค.
- ํ์ ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ฉด ํจ์ฌ ๋ ๋ง์ ์ค๋ฅ๋ฅผ ์ฐพ์๋ผ ์ ์๋ค.
const states = [
{name: 'Alabama', capitol: 'Montgomery'},
{name: 'Alaska', capitol: 'Juneau'},
{name: 'Arizona', capitol: 'Phoenix'},
// ...
];
for (const state of states) {
console.log(state.capital);
// ~~~~~~~ Property 'capital' does not exist on type// '{ name: string; capitol: string; }'.// Did you mean 'capitol'?
}
- ํ์ง๋ง ์ด์ฒ๋ผ ์ด๋์ชฝ์ด ์คํ์ธ์ง ํ๋จํ์ง ๋ชปํ ๋๊ฐ ์๋ค.
- ๋ฐ๋ผ์ ๋ช ์์ ์ผ๋ก states๋ฅผ ์ ์ธํด์ ์๋๋ฅผ ๋ถ๋ช ํ ํด์ผํ๋ค.
์ค๋ฅ๋ฐ์ ์ง์ ๋ ์ฌ๋ฐ๋ฅด๊ณ , ์ ์๋ ํด๊ฒฐ์ฑ ๋ ์ฌ๋ฐ๋ฅธ ์ฝ๋
interface State {
name: string;
capital: string;
}
const states: State[] = [
{name: 'Alabama', capitol: 'Montgomery'},
// ~~~~~~~~~~~~~~~~~~~~~
{name: 'Alaska', capitol: 'Juneau'},
// ~~~~~~~~~~~~~~~~~
{name: 'Arizona', capitol: 'Phoenix'},
// ~~~~~~~~~~~~~~~~~~ Object literal may only specify known// properties, but 'capitol' does not exist in type// 'State'. Did you mean to write 'capital'?// ...
];
for (const state of states) {
console.log(state.capital);
}
- ์๋๋ฅผ ๋ช ํํ ํด์ ํ์ ์คํฌ๋ฆฝํธ๊ฐ ์ ์ฌ์ ๋ฌธ์ ์ ์ ์ฐพ์ ์ ์๊ฒ ํจ.
// HIDE
export const foo = true;
interface State {
name: string;
capital: string;
}
// END
const states: State[] = [
{name: 'Alabama', capital: 'Montgomery'},
{name: 'Alaska', capitol: 'Juneau'},
// ~~~~~~~~~~~~~~~~~ Did you mean to write 'capital'?
{name: 'Arizona', capital: 'Phoenix'},
// ...
];
- ํ์
๊ตฌ๋ฌธ ์์ด ๋ฐฐ์ด ์์์ ๋ฑ ํ ๋ฒ capitol์ด๋ผ๊ณ ์ผ๋ค๋ฉด ์ค๋ฅ๊ฐ ๋์ง ์์
- ๊ทธ๋ฐ๋ฐ ํ์ ๊ตฌ๋ฌธ์ ์ถ๊ฐํ๋ฉด ์ค๋ฅ๋ฅผ ์ฐพ์ ์ ์์
ํ์ ์ฒด์ปค๋ฅผ ํต๊ณผํ ํ์ ์คํฌ๋ฆฝํธ ํ๋ก๊ทธ๋จ
ํ์ ์คํฌ๋ฆฝํธ ํ์ ์์คํ ์ ์๋ฐ์คํฌ๋ฆฝํธ์ ๋ฐํ์ ๋์์ '๋ชจ๋ธ๋ง' ํ๋ค.
const x = 2 + '3'; // OK, type is string
const y = '2' + 3; // OK, type is string
์ค๋ฅ๊ฐ ๋ ์ฝ๋์ด์ง๋ง, ํ์ ์คํฌ๋ฆฝํธ์ ํ์ ์ฒด์ปค๋ ์ ์์ผ๋ก ์ธ์ํ๋ค.
๋ฐ๋๋ก ์ ์ ๋์ํ๋ ์ฝ๋์ ์ค๋ฅ๋ฅผ ํ์ํ๊ธฐ๋ ํ๋ค.
const a = null + 7;// Evaluates to 7 in JS// ~~~~ Operator '+' cannot be applied to types ...const b = [] + 12;// Evaluates to '12' in JS// ~~~~~~~ Operator '+' cannot be applied to types ...
alert('Hello', 'TypeScript');// alerts "Hello"// ~~~~~~~~~~~~ Expected 0-1 arguments, but got 2
ํ์ ์คํฌ๋ฆฝํธ ์ฑํ ์ฌ๋ถ๋ ์ฐ๋ฆฌ๋ค์ ์ ํ
๋จ์ํ ๋ฐํ์ ๋์์ ๋ชจ๋ธ๋งํ๋ ๊ฒ ๋ฟ ์๋๋ผ ์๋์น ์์ ์ด์ํ ์ฝ๋๊ฐ ์ค๋ฅ๋ก ์ด์ด์ง ์ ์์
- ํ์ ์คํฌ๋ฆฝํธ์ ๋์์ ๋ฐ์ผ๋ฉด ์ค๋ฅ๊ฐ ์ ์ ์ฝ๋ ์์ฑ ๊ฐ๋ฅ
- null๊ณผ 7์ ๋ํ๋ค๊ฑฐ๋, []๊ณผ 12๋ฅผ ๋ํ๋ ๋ฑ์ ๋น์ฐํ๊ฒ ์ฌ๊ธฐ๋ฉด ํ์ ์คํฌ๋ฆฝํธ๋ฅผ ์ฐ์ง ์๋๊ฒ ์ข์
์์ฑ๋ ํ๋ก๊ทธ๋จ์ด ํ์ ์ฒดํฌ๋ฅผ ํต๊ณผํ๋๋ผ๋ ์ฌ์ ํ ๋ฐํ์์ ์ค๋ฅ ๋ฐ์ ๊ฐ๋ฅํ๋ค.
const names = ['Alice', 'Bob'];
console.log(names[2].toUpperCase());
TypeError: Cannot read property 'toUpperCase' of undefined
๋ฐฐ์ด์ด ๋ฒ์ ๋ด์์ ์ฌ์ฉ๋ ๊ฒ์ด๋ผ ๊ฐ์ ํ์ง๋ง ์ค์ ๋ก๋ X, ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค.
=> ํ์ ์คํฌ๋ฆฝํธ๊ฐ ์ดํดํ๋ ๊ฐ์ ํ์ ๊ณผ ์ค์ ๊ฐ์ ์ฐจ์ด๊ฐ ์๊ธฐ ๋๋ฌธ
Uploaded by N2T