[TIL 210822] ๐Ÿท๏ธTypescript Handbook์—์„œ ์ƒˆ๋กœ ์•Œ๊ฒŒ๋œ ์  2

Ko Seoyoungยท2021๋…„ 8์›” 23์ผ
0

โœ… Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
6/16
post-thumbnail

๐Ÿท๏ธTypescript Handbook์—์„œ ์ƒˆ๋กœ ์•Œ๊ฒŒ๋œ ์  2

Narrowing

function printAll(strs: string | string[] | null) {
  // BAD ๐Ÿ’ฉ
  // Null check๋ฅผ ์™œ ์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด ์•ˆ๋˜๋‚˜? ๋นˆ ๋ฌธ์ž์—ด์ธ ๊ฒฝ์šฐ๋ฅผ ๋ฌด์‹œํ•˜๊ธฐ ๋•Œ๋ฌธ (string ''์ธ ๊ฒฝ์šฐ)
  if (strs) {
    if (typeof strs === "object") {
      for (const s of strs) {
        console.log(s);
      }
    } else if (typeof strs === "string") {
      console.log(strs);
    }
  }

 // GOOD ๐Ÿ‘
 if (strs && typeof strs === "object") {
    for (const s of strs) {
      console.log(s);
    }
  } else if (typeof strs === "string") {
    console.log(strs);
  }
} 
  1. == null ์€ null๊ณผ undefined๋ฅผ ๋ชจ๋‘ ์ฒดํฌํ•œ๋‹ค. (== undefined ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€) (=== ์™€๋Š” ๊ตฌ๋ถ„๋จ)
  2. in ๊ฐ์ฒด ์†์„ฑ์ด ๋“ค์–ด์žˆ๋Š”์ง€ ์•„๋‹Œ์ง€ ์—ฌ๋ถ€๋กœ type narrowing์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
type Fish = { swim: () => void };
type Bird = { fly: () => void };
 
function move(animal: Fish | Bird) {
  if ("swim" in animal) {
    return animal.swim();
  }
 
  return animal.fly();
}
  1. instanceof๋„ ํƒ€์ž…๊ฐ€๋“œ๋กœ ์“ฐ์ž„ (if (x instanceof Date))
  2. never type
type Shape = Circle | Square;
 
function getArea(shape: Shape) {
  switch (shape.kind) {
    case "circle":
      return Math.PI * shape.radius ** 2;
    case "square":
      return shape.sideLength ** 2;
    default:
			// ์ด๋”ฐ shape์€ circle์™€ square์— ์˜ํ•œ ํƒ€์ž…๊ฐ€๋“œ๊ฐ€ ์ง€์ •๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์—, 
			// ๋” ์ด์ƒ ์˜ฌ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์ด ์—†์–ด never ํƒ€์ž…์ด๋œ๋‹ค.
      const _exhaustiveCheck: never = shape;
      return _exhaustiveCheck;
  }
}
function fail(msg: string): never {
  throw new Error(msg);
}

More on Functions

  1. Constraints๋Š” ํƒ€์ž… ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋ฐ›์•„๋“ค์ผ ์ˆ˜ ์žˆ๋Š” ํƒ€์ž…์˜ ์ข…๋ฅ˜๋ฅผ ์ œํ•œ์‹œํ‚ค๊ธฐ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.
function longest<Type extends { length: number }>(a: Type, b: Type) {
  if (a.length >= b.length) {
    return a;
  } else {
    return b;
  }
}
 
// longerArray is of type 'number[]'
const longerArray = longest([1, 2], [1, 2, 3]);
// longerString is of type 'string'
const longerString = longest("alice", "bob");
// Error! Numbers don't have a 'length' property
const notOK = longest(10, 100); // ์ˆซ์ž๋Š” length ์†์„ฑ์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— ์—๋Ÿฌ
  1. callback ํ•จ์ˆ˜์— ํƒ€์ดํ•‘์„ ํ•  ๋•, ๋˜๋„๋ก optional ํŒŒ๋ผ๋ฏธํ„ฐ๋Š” ์‚ฌ์šฉํ•˜์ง€ ๋ง์•„์•ผ ํ•œ๋‹ค.
  2. unknown: any value๋ฅผ ๋Œ€ํ‘œํ•œ๋‹ค. any ํƒ€์ž…๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ unknown value๋กœ๋Š” ์•„๋ฌด ๋™์ž‘์ด๋‚˜ ์ œ์•ฝ์—†์ด ๋Š” ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— any๋ณด๋‹ค ๋” ์•ˆ์ „ํ•˜๋‹ค.
  3. rest ํ˜•ํƒœ๋กœ ํ•จ์ˆ˜์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋„˜๊ฒจ์ค„์ˆ˜ ์žˆ๋‹ค.
const arr1 = [1, 2, 3];
const arr2 = [4, 5, 6];
arr1.push(...arr2); // arr1.push(1,2,3)๊ณผ ๊ฐ™์Œ
  1. void ๋ฐ˜ํ™˜ ์œ ํ˜•(type vf = () => void)์ด ์žˆ๋Š” ์ปจํ…์ŠคํŠธ ํ•จ์ˆ˜ ์œ ํ˜•์€ ๊ตฌํ˜„๋  ๋•Œ ๋‹ค๋ฅธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๋ฌด์‹œ๋œ๋‹ค! (๋”ฐ๋ผ์„œ, void ํƒ€์ž…์€ undefined๊ณผ ๊ฐ™์ง€ ์•Š๋‹ค.)

Object Types

  1. type ColorfulCircle = Colorful & Circle; - Intersection Types (&)

    (Colorful๊ณผ Circle์€ interface. (์šฉ์–ด๋ฅผ ๋ชฐ๋ž์–ด์„œ ์ ์Œ))

  2. ๋ณ€๊ฒฝ๋˜๋ฉด ์•ˆ๋˜๋Š” ๋ฐฐ์—ด์ธ ๊ฒฝ์šฐ ReadonlyArray ์ œ๋„ˆ๋ฆญ ํƒ€์ž…์„ ์‚ฌ์šฉํ•œ๋‹ค.

Creating Types from Types

  1. Generic ์˜ˆ์‹œ
// ํŒฉํ† ๋ฆฌ ํŒจํ„ด์—์„œ ์ƒ์„ฑ์ž๊ฐ€ createํ•จ์ˆ˜๋กœ ๋„˜๊ฒจ์ง€๋Š” ๊ฒƒ ํƒ€์ดํ•‘
function create<Type>(c: { new (): Type }): Type {
  return new c();
}
  1. ์•ž์— -๋‚˜ +๋ฅผ ๋ถ™์—ฌ์„œ readonly๋ฅผ ๋นผ๊ฑฐ๋‚˜ ์˜ต์…”๋„(?)์„ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.
type CreateMutable<Type> = {
  -readonly [Property in keyof Type]: Type[Property];
};

// ๋ชจ๋“  ์†์„ฑ์ด optionalํ•˜์ง€ ์•Š๊ฒŒ ๋จ
type Concrete<Type> = {
  [Property in keyof Type]-?: Type[Property];
};
  1. as๋กœ key๋ฅผ ๋‹ค์‹œ ๋งตํ•‘ ํ•  ์ˆ˜ ๋„ ์žˆ๋‹ค.
type Getters<Type> = {
    [Property in keyof Type as `get${Capitalize<string & Property>}`]: () => Type[Property]
};
// Type์˜ key๊ฐ€ name, age, location์ด๋ผ๋ฉด
// Getters ํƒ€์ž…์˜ key๋“ค์€ getName, getAge, getLocation์ด ๋œ๋‹ค.
  1. Template Literal Types
    • Uppercase : ๋ฌธ์ž์—ด ํƒ€์ž…์„ ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€๊ฒฝํ•œ ๋ฌธ์ž์—ด ํƒ€์ž…์œผ๋กœ ๋ฐ”๊ฟ”์ค€๋‹ค.
    • Lowercase : ์–˜๋Š” ์†Œ๋ฌธ์ž๋กœ
    • Capitalize : ์ฒซ๊ธ€์ž๋งŒ ๋Œ€๋ฌธ์ž๋กœ
    • Uncapitalize : ์ฒซ๊ธ€์ž๋งŒ ์†Œ๋ฌธ์ž๋กœ

  • any ๋Œ€์‹  unknown์„ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, generics ํƒ€์ž…์œผ๋กœ ๋Œ€์ฒดํ•  ๋ฐฉ๋ฒ• ์ƒ๊ฐํ•ด๋ณด๊ธฐ
profile
Web Frontend Developer ๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป #React #Nextjs #ApolloClient

0๊ฐœ์˜ ๋Œ“๊ธ€