π 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