์žฌ๋ฐŒ๊ฒŒ ํ•ฉ์‹œ๋‹ค

๊ธฐ๋กํ•˜๊ธฐ, ๊ฐ€์‹œํ™”ํ•˜๊ธฐ

ํ”„๋กœ์ ํŠธ ๊ธฐ๋ก/YSS | ์—ฐ์„ธ๋Œ€ํ•™๊ต ๊ณต๊ฐ„๋Œ€๊ด€ ์„œ๋น„์Šค

(์—๋Ÿฌ ํ•ด๊ฒฐ) Union type๊ณผ ํƒ€์ž… ๊ฐ€๋“œ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๊ธฐ

์€๋˜๋”” 2022. 9. 19. 01:40

Union type

| ์—ฐ์‚ฐ์ž๋กœ ์—ฌ๋Ÿฌ ๊ฐœ์˜ ํƒ€์ž…์ด ์—ฐ๊ฒฐ๋œ ํƒ€์ž…์„ ์˜๋ฏธํ•œ๋‹ค. ์ด๋•Œ | ์—ฐ์‚ฐ์ž๋Š” or์˜ ์˜๋ฏธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค. ์ด๋Ÿฌํ•œ Union type์„ ํ™œ์šฉํ•ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜๊ณ ์ž ํ–ˆ๋‹ค.

๋ฌธ์ œ ์ •์˜

interface Props {
  data: LectureRoomsPerBuilding | LectureRoomInfo;
  children?: LectureRoomInfo[];
}

๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ํƒ€์ž… ์ถ”๋ก ์—์„œ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ๋‹ค.

export interface LectureRoomInfo {
  room_name: string;
  room_uid: RoomUID;
}

export interface LectureRoomsPerBuilding {
  building_name: string;
  building_uid: number;
  rooms: LectureRoomInfo[];
}

์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•œ ์ฝ”๋“œ๋Š” data.room_name data.building_name ์ด์—ˆ๋‹ค. "LectureRoomsPerBuilding์€ room_name ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค." "LectureRoomInfo๋Š” building_name ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•˜์ง€ ์•Š๋Š”๋‹ค." ๋ผ๋Š” ์—๋Ÿฌ ๋ฉ”์‹œ์ง€๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค. or์„ ์˜๋ฏธํ•˜๋Š” | ์—ฐ์‚ฐ์ž๋กœ ๋‘ ํƒ€์ž…์„ ์—ฐ๊ฒฐ ํ–ˆ์œผ๋‹ˆ ๋‘ ์ธํ„ฐํŽ˜์ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋“ค์„ ๋ชจ๋‘ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„๊ฑฐ๋ผ ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ๋‚ด ์˜ˆ์ƒ์ด ํ‹€๋ ธ์—ˆ๋‹ค.

Union Type์— ๋Œ€ํ•ด ๊ตฌ๊ธ€๋งํ•œ ๊ฒฐ๊ณผ ์ด์™€ ๊ด€๋ จ๋œ ์ž๋ฃŒ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

Union Type ์‚ฌ์šฉ ์‹œ ์ฃผ์˜์ 

https://joshua1988.github.io/ts/guide/operator.html#intersection-type

interface Person {
  name: string;
  age: number;
}
interface Developer {
  name: string;
  skill: string;
}
function introduce(someone: Person | Developer) {
  someone.name; // O ์ •์ƒ ๋™์ž‘
  someone.age; // X ํƒ€์ž… ์˜ค๋ฅ˜
  someone.skill; // X ํƒ€์ž… ์˜ค๋ฅ˜
}

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ ์ž…์žฅ์—์„œ๋Š” introduce ํ•จ์ˆ˜ ์‹คํ–‰ ์‹œ ์ธ์ž๋กœ Person์ด ์˜ฌ ์ง€, Developer๊ฐ€ ์˜ฌ ์ง€ ์•Œ ์ˆ˜ ์—†๋‹ค. ๋”ฐ๋ผ์„œ ๋‘˜ ์ค‘ ์–ด๋Š ํƒ€์ž…์ด ์™€๋„ ์˜ค๋ฅ˜๊ฐ€ ๋‚˜์ง€ ์•Š๋Š” ๋ฐฉํ–ฅ์œผ๋กœ ํƒ€์ž…์„ ์ถ”๋ก ํ•œ๋‹ค. ๊ฐ€๋ น ์ธ์ž๋กœ Developer ํƒ€์ž…์ด ์˜จ๋‹ค๋ฉด someone.age ๋ผ๋Š” ์ฝ”๋“œ๋Š” ์—๋Ÿฌ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๊ธฐ ๋•Œ๋ฌธ์— ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ๊ฐ€ ๋ฏธ๋ฆฌ ์—๋Ÿฌ๋ฅผ ๋„์šฐ๋Š” ๊ฒƒ์ด๋‹ค.

ํƒ€์ž… ๊ฐ€๋“œ

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„  ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ์ด์šฉํ•ด์•ผ ํ•œ๋‹ค.

ํƒ€์ž…๊ฐ€๋“œ๋ž€? ํ•จ์ˆ˜ ๋‚ด์—์„œ ์กฐ๊ฑด๋ฌธ์„ ์‚ฌ์šฉํ•ด ํƒ€์ž…์„ ์ขํžˆ๋Š” ๊ฒƒ

function Hello(user: string|number){
  if (typeof user === 'string'){
    // ํƒ€์ž…์„ string์œผ๋กœ ์ขํžŒ๋‹ค
    console.log(`Hello ${user}`);
  }
  console.log(`Hello ${users[user]}`);
}

ํฌ๊ฒŒ ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ๋‹ค.

  • typeof: ์›์‹œ ์ž๋ฃŒํ˜•๋งŒ ๊ตฌ๋ถ„ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • in: ๊ฐ์ฒด์— ํŠน์ • ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•˜๋Š”์ง€๋ฅผ ํ™•์ธํ•จ์œผ๋กœ์จ ํƒ€์ž…์„ ์ขํžŒ๋‹ค.

๋‚˜๋Š” LectureRoomInfo์™€ LectureRoomsPerBuilding์„ ๊ตฌ๋ถ„ํ•ด์•ผ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์—, ๋‘๋ฒˆ์งธ ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.

ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ์ ์šฉํ•œ ํ•จ์ˆ˜

  const updateLectureRoomChoice = () => {
    if ('room_name' in data) {
      setLectureRoomChoice({
        building_uid: data.building_uid,
        room_uid: data.room_uid,
      });
    }
  };

props data์— room_name ํ”„๋กœํผํ‹ฐ๊ฐ€ ์กด์žฌํ•œ๋‹ค == data๊ฐ€ LectureRoomInfo ํƒ€์ž…์ด๋‹ค. == ์„œ๋ธŒ๋ฉ”๋‰ด์— ํ•ด๋‹นํ•œ๋‹ค.

 

์ €๋ฒˆ ๊ธ€์—์„œ๋Š” ๋ฉ”์ธ๋ฉ”๋‰ด์™€ ์„œ๋ธŒ๋ฉ”๋‰ด๋ฅผ children props์˜ ์—ฌ๋ถ€๋กœ ๊ตฌ๋ถ„ํ–ˆ์—ˆ๋Š”๋ฐ, ์ด๋ ‡๊ฒŒ ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ํ™œ์šฉํ•ด ๊ตฌ๋ถ„ํ•  ์ˆ˜๋„ ์žˆ์—ˆ๋‹ค. ๋‹ค๋งŒ ํƒ€์ž… ๊ฐ€๋“œ์˜ ๊ฒฝ์šฐ jsx์—์„œ ๊ณง๋ฐ”๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ UI์—์„œ๋Š” children props์„, ํ•จ์ˆ˜ ๋‚ด์—์„œ๋Š” ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ์‹์„ ํƒํ•ด์•ผ๊ฒ ๋‹ค.

 

์œ„ ์˜ˆ์‹œ์ฒ˜๋Ÿผ data์˜ ํ”„๋กœํผํ‹ฐ์— ์ ‘๊ทผํ•˜๋Š” ํ•จ์ˆ˜๋“ค์— ๋ชจ๋‘ ํƒ€์ž… ๊ฐ€๋“œ๋ฅผ ์ ์šฉํ•ด ์คฌ๋”๋‹ˆ ์—๋Ÿฌ๊ฐ€ ํ•ด๊ฒฐ๋˜์—ˆ๋‹ค!