Skip to content

Latest commit

Β 

History

History
239 lines (152 loc) Β· 7.87 KB

2021-12-09-effectiveTypescript-item12.md

File metadata and controls

239 lines (152 loc) Β· 7.87 KB

μ΄νŽ™ν‹°λΈŒ νƒ€μž…μŠ€ν¬λ¦½νŠΈ

  • λŒ„ λ°΄λ”μΊ„μ˜ μ΄νŽ™ν‹°λΈŒ νƒ€μž…μŠ€ν¬λ¦½νŠΈ λ„μ„œμ˜ μ•„μ΄ν…œ11,12 읽고 μ •λ¦¬ν•œ λ‚΄μš©μž…λ‹ˆλ‹€.

μ•„μ΄ν…œ 12. ν•¨μˆ˜ ν‘œν˜„μ‹μ— νƒ€μž… μ μš©ν•˜κΈ°

  • νƒ€μž…μŠ€ν¬λ¦½νŠΈμ—μ„œλŠ” ν•¨μˆ˜ μ„ μ–Έλ¬Έ 보닀 ν•¨μˆ˜ ν‘œν˜„μ‹μ„ μ‚¬μš©ν•˜λŠ” 것이 μ’‹μŠ΅λ‹ˆλ‹€.
  • κ·Έ μ΄μœ λŠ” ν•¨μˆ˜ νƒ€μž… 선언은 ν•¨μˆ˜ ν‘œν˜„μ‹μ— μž¬μ‚¬μš© ν•  수 μžˆλŠ” 이점이 있기 λ•Œλ¬Έμž…λ‹ˆλ‹€.

ν•¨μˆ˜ νƒ€μž… μ„ μ–Έμ˜ μž₯점

예제 1. DiceRollFn ν•¨μˆ˜ νƒ€μž…μ„ μž¬μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜ μ„ μ–Έλ¬Έ

function rollDice1(sides: number): number {
  /* COMPRESS */ return 0; /* END */
} // Statement

ν•¨μˆ˜ ν‘œν˜„μ‹

type DiceRollFn = (sides: number) => number;
const rollDice: DiceRollFn = (sides) => {
  /* COMPRESS */ return 0; /* END */
};

예제 2. lib.dom.d.ts 에 μ •μ˜λ˜μ–΄ μžˆλŠ” fetch νƒ€μž… μ„ μ–Έ ν•¨μˆ˜ νƒ€μž…μ„ μž¬μ‚¬μš©ν•˜μ—¬ κ°„κ²°ν•˜κ²Œ μž‘μ„±ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

ν•¨μˆ˜ μ„ μ–Έλ¬Έ

async function checkedFetch(input: RequestInfo, init?: RequestInit) {
  const response = await fetch(input, init);
  if (!response.ok) {
    throw Error("request failed:", response.status);
  }

  return response;
}

async function getQuote() {
  const response = await checkedFetches('/quote?by=Mark+Twain');
  const quote = await response.json();
  return quote;
}

ν•¨μˆ˜ ν‘œν˜„μ‹

const checkedFetch: typeof fetch = async (input, init) => {
  const resp = await fetch(input, init);
  if (!response.ok) {
    throw Error("request failed:", response.status);
  }

  return response;
};

async function getQuote() {
  const response = await checkedFetches('/quote?by=Mark+Twain');
  const quote = await response.json();
  return quote;
}
  • fetch νƒ€μž…μ€ λ°˜ν™˜ νƒ€μž…κΉŒμ§€ 보μž₯ ν•©λ‹ˆλ‹€. 예λ₯Ό λ“€μ–΄ throw λŒ€μ‹  return 을 ν•˜κ²Œ 되면 Promise<Response> μ—μ„œ Promise<Response|Error> νƒ€μž…μ΄ λ˜μ–΄λ²„λ¦¬κΈ° λ•Œλ¬Έμ—, μ—λŸ¬λ₯Ό λƒ…λ‹ˆλ‹€.
const checkedFetch: typeof fetch = async (input, init) => {
  const resp = await fetch(input, init);
  if (!response.ok) {
    return Error("request failed:", response.status); //❗️
  }

  return response;
};

async function getQuote() {
  const response = await checkedFetches('/quote?by=Mark+Twain');
  const quote = await response.json();
  return quote;
}

Screen Shot 2021-12-10 at 1 42 49 AM

μ•„μ΄ν…œ 13. νƒ€μž…κ³Ό μΈν„°νŽ˜μ΄μŠ€μ˜ 차이 μ•ŒκΈ°

λŒ€λΆ€λΆ„μ˜ 경우 νƒ€μž…μ„ μ‚¬μš©ν•΄λ„λ˜κ³  μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•΄λ„ λ©λ‹ˆλ‹€. ν•˜μ§€λ§Œ λ‘˜μ˜ 차이λ₯Ό μ•Œκ³  λ™μΌν•œ λ°©λ²•μœΌλ‘œ νƒ€μž…μ„ μ •μ˜ν•΄ 일관성을 μœ μ§€ν•΄μ•Ό ν•©λ‹ˆλ‹€.

차이점.

1. λ‘˜ λͺ¨λ‘ ν™•μž₯이 κ°€λŠ₯ν•˜λ‚˜ ν™•μž₯의 κ°œλ…μ΄ μ„œλ‘œ λ‹€λ¦…λ‹ˆλ‹€.

type StateType = {
  name: string;
  capital: string;
};

interface StateInterface {
  name: string;
  capital: string;
}

interface StateLocation {
  latitude: number;
}

interface StateWithPopInterface extends StateInterface {
  population: number;
}
type StateWithPopType = StateLocation & StateWithPopInterface;

const stateA: StateWithPopInterface = {};
const stateB: StateWithPopType = {};
  • μΈν„°νŽ˜μ΄μŠ€ νƒ€μž…μ„ 가진 stateA에선 name,capital,population λͺ¨λ‘ μ„ μ–Έλ˜μ–΄μžˆμ§€ μ•Šλ‹€κ³  μ—λŸ¬λ₯Ό λ‚΄μ£Όκ³  μžˆμŠ΅λ‹ˆλ‹€.

Screen Shot 2021-12-09 at 11 39 03 PM

  • type ν‚€μ›Œλ“œλ₯Ό 가진 stateB에선 name,capital,population,latitudeκΉŒμ§€ μ—λŸ¬λ₯Ό λ‚΄μ•Όν•œλ‹€κ³  μ˜ˆμƒμ„ ν•˜μ§€λ§Œ, 싀지 name,capital,population 만 μ—λŸ¬λ₯Ό λ‚΄μ£Όκ³  μžˆμŠ΅λ‹ˆλ‹€.

Screen Shot 2021-12-09 at 11 39 15 PM

즉, μΈν„°νŽ˜μ΄μŠ€λŠ” μœ λ‹ˆμ˜¨ νƒ€μž… 같은 λ³΅μž‘ν•œ νƒ€μž…μ„ ν™•μž₯ν•˜μ§€λŠ” λͺ»ν•©λ‹ˆλ‹€. λ³΅μž‘ν•œ νƒ€μž…μ˜ ν™•μž₯을 원할 μ‹œ, νƒ€μž…κ³Ό &λ₯Ό μ‚¬μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

μ°Έκ³ 
νƒ€μž…μ€ μΈν„°νŽ˜μ΄μŠ€λ₯Ό ν™•μž₯ κ°€λŠ₯ν•˜λ―€λ‘œ, νƒ€μž… μ •μ˜μ‹œ μΈν„°νŽ˜μ΄μŠ€λ„ μ‚¬μš© κ°€λŠ₯ ν•©λ‹ˆλ‹€.

2. μΈν„°νŽ˜μ΄μŠ€μ—λŠ” μœ λ‹ˆμ˜¨μ„ ν•  μˆ˜κ°€ μ—†μŠ΅λ‹ˆλ‹€. μœ λ‹ˆμ˜¨ νƒ€μž…λ§Œ κ°€λŠ₯ν•©λ‹ˆλ‹€.

type AorB = "a" | "b"; //βœ… νƒ€μž… λ³„μΉ­μ—μ„œλ§Œ κ°€λŠ₯, 
type Input = {
  /* ... */
};
type Output = {
  /* ... */
};

type NamedVariable = (Input | Output) & { name: string }; //βœ… νƒ€μž… λ³„μΉ­μ—μ„œλ§Œ κ°€λŠ₯ 

3. μΈν„°νŽ˜μ΄μŠ€μ—λŠ” νƒ€μž…μ— μ—†λŠ” 보강(augment)이 κ°€λŠ₯ν•©λ‹ˆλ‹€.

interface State {
  name: string;
  capital: string;
}

interface State {
  population: number;
}
const wyoming: State = {
  name: 'Wyoming',
  capital: 'Cheyenne',
  population: 500_000
};  // βœ… OK
  • λ§Œμ•½ State λ₯Ό type 별칭을 μ‚¬μš©ν•˜μ˜€λ‹€λ©΄ μ—λŸ¬λ₯Ό λƒ…λ‹ˆλ‹€.

Screen Shot 2021-12-10 at 12 18 57 AM

  • 같은 μ΄λ¦„μ˜ μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš©ν•˜μ—¬ 속성을 ν™•μž₯ν•˜λŠ” 것을 μ„ μ–Έ 병합(declaration merging) 이라고 ν•©λ‹ˆλ‹€.

  • μΈν„°νŽ˜μ΄μŠ€λŠ” νƒ€μž…μ„ μ–Έ 파일 μž‘μ„±μ‹œ 선언병합을 μœ„ν•΄ μ‚¬μš© λ©λ‹ˆλ‹€.

  • 예) Array μΈν„°νŽ˜μ΄μŠ€ tsconfig의 "target": "ES5" 일 경우,

    • array 의 μΈν„°νŽ˜μ΄μŠ€λŠ” lib.es5.ts 에 μ„ μ–Έλœ μΈν„°νŽ˜μ΄μŠ€κ°€ μ‚¬μš©λ©λ‹ˆλ‹€.
    Screen Shot 2021-12-10 at 12 27 52 AM Screen Shot 2021-12-10 at 1 09 11 AM

    tsconfig νŒŒμΌλ‚΄ lib:["esnext"] μΆ”κ°€μ‹œ lib.es.2015.d.ts 에 μ„ μ–Έλœ μΈν„°νŽ˜μ΄μŠ€κ°€ λ³‘ν•©λ©λ‹ˆλ‹€.

    Screen Shot 2021-12-10 at 1 01 36 AM Screen Shot 2021-12-10 at 1 05 49 AM

    결과적으둜 각 선언이 λ³‘ν•©λ˜μ–΄ 전체 λ©”μ„œλ“œλ₯Ό κ°€μ§€λŠ” Arrayλ₯Ό κ°–κ²Œ λ©λ‹ˆλ‹€.

μ–΄λ–€ 것을 μ‚¬μš©ν•΄μ•Ό ν• κΉŒ?

  • λ³΅μž‘ν•œ νƒ€μž… 일 경우 :

    βœ”οΈ νƒ€μž… 별칭 μ‚¬μš©

  • νƒ€μž…κ³Ό μΈν„°νŽ˜μ΄μŠ€ 두가지 λ°©λ²•μœΌλ‘œ ν‘œν˜„ν•  수 μžˆλŠ” κ°„λ‹¨ν•œ 객체일 경우:

    βœ”οΈ 일관성과 λ³΄κ°•μ˜ κ΄€μ μ—μ„œ κ³ λ € ν•  ν•„μš”κ°€ μžˆμŠ΅λ‹ˆλ‹€.

  • μŠ€νƒ€μΌμ΄ ν™•λ¦½λ˜μ§€ μ•Šμ€ ν”„λ‘œμ νŠΈμΈ 경우 :

    βœ”οΈ API 에 λŒ€ν•œ νƒ€μž…μ„ μ–ΈμΌ 경우 μΈν„°νŽ˜μ΄μŠ€λ₯Ό μ‚¬μš© ν•©λ‹ˆλ‹€. (병합에 유용)

    βœ”οΈ κ·ΈλŸ¬λ‚˜ ν”„λ‘œμ νŠΈ λ‚΄λΆ€μ μœΌλ‘œ μ‚¬μš©λ˜λŠ” νƒ€μž…μ— μ„ μ–Έ 병합이 λ°œμƒν•˜λŠ” 것은 잘λͺ»λœ 섀계 μž…λ‹ˆλ‹€β—οΈ -> νƒ€μž… μ‚¬μš© ꢌ고.

정리

  • λ™μΌν•œ νƒ€μž… μ‹œκ·Έλ‹ˆμ²˜λ₯Ό κ°€μ§€λŠ” μ—¬λŸ¬κ°œμ˜ ν•¨μˆ˜λ₯Ό μž‘μ„±ν•  λ•ŒλŠ” λ§€κ°œλ³€μˆ˜μ˜ νƒ€μž…κ³Ό λ°˜ν™˜νƒ€μž…μ„ λ°˜λ³΅ν•΄μ„œ μž‘μ„±ν•˜λŠ” 것보닀 ν•¨μˆ˜μ „μ²΄μ˜ νƒ€μž…μ„ μ–Έμ„ μ μš©ν•΄μ•Ό ν•©λ‹ˆλ‹€.

  • νƒ€μž…κ³Ό μΈν„°νŽ˜μ΄μŠ€μ˜ 차이λ₯Ό μ΄ν•΄ν•˜κ³  ν”„λ‘œμ νŠΈμ—μ„œ μ–΄λ–€ 문법을 μ‚¬μš©ν• μ§€ κ²°μ •ν•  λ•Œ 일관성과 보강기법을 κ³ λ €ν•΄μ•Όν•©λ‹ˆλ‹€.


참고자료