Among Us - Crewmates
 

__[TypeScript] 01. νƒ€μž…μŠ€ν¬λ¦½νŠΈμ™€ μžλ°”μŠ€ν¬λ¦½νŠΈ 관계 μ΄ν•΄ν•˜κΈ°__

728x90

πŸ“š Effective TypeScript 책을 읽고 κ³΅λΆ€ν•œ λ‚΄μš©μ„ μ •λ¦¬ν•œ κΈ€μž…λ‹ˆλ‹€.

πŸ’‘
νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ μƒμœ„μ§‘ν•©(superset)이닀

νƒ€μž…μŠ€ν¬λ¦½νŠΈμ™€ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ 관계

  • νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” λ¬Έλ²•μ μœΌλ‘œλ„ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ μƒμœ„μ§‘ν•©
  • μžλ°”μŠ€ν¬λ¦½νŠΈ ν”„λ‘œκ·Έλž¨μ— 문법 였λ₯˜κ°€ μ—†λ‹€λ©΄
    • μœ νš¨ν•œ νƒ€μž…μŠ€ν¬λ¦½νŠΈ ν”„λ‘œκ·Έλž¨μ΄λΌκ³  ν•  수 μžˆλ‹€.

λ¬Έλ²•μ˜ μœ νš¨μ„±κ³Ό λ™μž‘μ˜ μ΄μŠˆλŠ” 독립적인 문제

  • μžλ°”μŠ€ν¬λ¦½νŠΈ ν”„λ‘œκ·Έλž¨μ— μ–΄λ–€ μ΄μŠˆκ°€ μ‘΄μž¬ν•œλ‹€λ©΄ 문법 였λ₯˜κ°€ μ•„λ‹ˆλ”λΌλ„ νƒ€μž… μ²΄μ»€μ—κ²Œ 지적 κ°€λŠ₯μ„± λ†’μŒ
  • ν•˜μ§€λ§Œ, νƒ€μž…μŠ€ν¬λ¦½νŠΈλŠ” μ—¬μ „νžˆ μž‘μ„±λœ μ½”λ“œλ₯Ό νŒŒμ‹±ν•˜κ³  μžλ°”μŠ€ν¬λ¦½νŠΈλ‘œ λ³€ν™˜ κ°€λŠ₯

ν™•μž₯자의 차이

  • μžλ°”μŠ€ν¬λ¦½νŠΈ νŒŒμΌμ€ .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

728x90
λ°˜μ‘ν˜•