TIL/Typescript

[TIL] TypeScript์—์„œ Record ํƒ€์ž… ํ™œ์šฉ

Dong _ hwa 2023. 11. 5. 04:44

 

ํƒ€์ž… ์ง€์ •์— ์–ด๋ ค์›€์ด ์žˆ์–ด ์„œ์น˜ํ•˜๋˜ ์ค‘ Record๋ผ๋Š” ๊ฒƒ์„ ๋ฐœ๊ฒฌํ•ด์„œ ์ •๋ฆฌํ•ด๋ณด์•˜๋‹ค

"Record" ํƒ€์ž…์€ ๊ฐ์ฒด ํƒ€์ž…์„ ์ •์˜ํ•  ๋•Œ ์‚ฌ์šฉ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.

 

- Record<Key, Value>

type RecordEx = Record<string, number>;


์œ„์— ์˜ˆ์‹œ๋Š”, RecordEx๋Š” ๋ฌธ์ž์—ด ํ‚ค์™€ ์ˆซ์ž ๊ฐ’์œผ๋กœ ์ด๋ฃจ์–ด์ง„ ๊ฐ์ฒด๋ฅผ ๋งํ•œ๋‹ค.

string์€ ํ‚ค์˜ ํƒ€์ž…์ด๊ณ  number๋Š” ๊ฐ’์˜ ํƒ€์ž…์ธ ๊ฒƒ.

 

๋Œ€์ถฉ ์ฃผ๋กœ ์ด๋Ÿฐ ์‹์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ

// ์‚ฌ์šฉ์ž ์ •๋ณด ๊ฐ์ฒด
type User = Record<"id" | "username" | "email", string>;

// ๋„์‹œ๋ณ„ ์ธ๊ตฌ์ˆ˜
type PopulationByCity = Record<"Seoul" | "New York" | "Tokyo", number>;

 

์‚ฌ์šฉ์ž ์ •๋ณด๊ฐ์ฒด์™€ ๋„์‹œ๋ณ„ ์ธ๊ตฌ์ˆ˜์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋Š”

const users: User = {
  user1: { id: "1", username: "Ahri", email:"pop_star@aa.com" },
  user2: { id: "2", username: "Zoe", email:"bubble@aa.com" },
  user3: { id: "3", username: "Izreal", email:"lux@aa.com" },
};

const cityPopulations: PopulationByCity = {
  Seoul: 9720846,    
  New York: 8398748,  
  Tokyo: 13929286     
};

 

์ด๋Ÿฐ์‹์œผ๋กœ ๋˜์–ด์žˆ์„ ๊ฒƒ์ธ๋ฐ ์ด๋ ‡๊ฒŒ ๊ฐ„๋‹จํ•˜๊ฒŒ ํ‘œํ˜„์ด ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

Record๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ํƒ€์ž…์„ ์ง€์ •ํ•˜๊ฒŒ ๋˜์—ˆ๋‹ค๋ฉด,

interface User {
  id: string;
  username: string;
  email: string;
// or
  [key: string]: string;
}

interface PopulationByCity {
  [key: string]: number;
}

 

์ด๋Ÿฐ์‹์œผ๋กœ ์ ์—ˆ์„ ๊ฒƒ์ด๋‹ค.

๊ทผ๋ฐ ์‚ฌ์‹ค ํ‚ค-๋ฐธ๋ฅ˜๊ฐ€ ์ด๋ ‡๊ฒŒ ๋Š˜ ๊ฐ™์€ ๊ฒƒ๋„ ์•„๋‹ˆ๊ณ  ๋งŒ์•ฝ์— id๊ฐ’์ด number๋ผ๋ฉด ๊ตณ์ด Record๋ฅผ ์“ฐ์ง€ ์•Š์•„๋„ ๋  ๊ฒƒ์ด๋‹ค

- ๋™์ผํ•œ ํƒ€์ž…์„ ๊ฐ€์ง„ ์—ฌ๋Ÿฌ ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์ •์˜ํ•ด์•ผ ํ•  ๋•Œ
- ๊ฐ์ฒด์˜ ํ‚ค๋ฅผ ๋™์ ์œผ๋กœ ์ƒ์„ฑํ•˜๊ณ , ํ•ด๋‹น ํ‚ค์™€ ๊ฐ’์„ ๋งคํ•‘ํ•  ๋•Œ.

 

๋” ์œ ์šฉํ•˜๊ฒŒ ์“ฐ์ธ๋‹ค. ํ•˜์ง€๋งŒ ๋ญ˜ ์“ฐ๋“  ์ž์œ !

 

๋™์ ์œผ๋กœ ํ”„๋กœํผํ‹ฐ ์ƒ์„ฑ
function createDynamicRecord(keys: string[]): Record<string, number> {
  const dynamicRecord: Record<string, number> = {};
  keys.forEach((key, index) => {
    dynamicRecord[key] = index;
  });
  return dynamicRecord;
}

const dynamicData = createDynamicRecord(["one", "two", "three"]);

// dynamicData: { one: 0, two: 1, three: 2 }

ํ‚ค-๊ฐ’ ์Œ์„ ๊ณ„์† ๋™์ ์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ด๊ฒƒ๋„ ๋งˆ์ฐฌ๊ฐ€์ง€๊ณ  Record๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„๋„ ๋˜์ง€๋งŒ, ๋ถˆํ•„์š”ํ•œ ๋ฐ˜๋ณต์€ ์ค„์ผ ์ˆ˜ ์žˆ๋‹ค.

 

 

ํ‚ค-๊ฐ’ ๋ณ€ํ™˜
type InvertedRecord<T extends Record<string, any>> = {
  [K in keyof T]: K;
};

const fruitNames = {
  apple: "red",
  banana: "yellow",
  orange: "orange",
};

type InvertedFruitNames = InvertedRecord<typeof fruitNames>;

// InvertedFruitNames: { apple: "apple"; banana: "banana"; orange: "orange"; }

 

๊ธฐ์กด ๊ฐ์ฒด์˜ ํ‚ค๋ฅผ ํ•ด๋‹น ํ‚ค์™€ ๋™์ผํ•œ ๊ฐ’์œผ๋กœ ๋ณ€ํ™˜ํ•˜๋Š” ์˜ˆ์‹œ์ด๋‹ค.

  • InvertedRecord ํƒ€์ž…์€ ์ œ๋„ค๋ฆญ T๋ฅผ ๋ฐ›์•„์™€์„œ T์˜ ํ‚ค-๊ฐ’ ์Œ์„ ๋ณ€ํ™˜
  • InvertedFruitNames๋Š” InvertedRecord๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ fruitNames ๊ฐ์ฒด์˜ ํ‚ค-๊ฐ’ ์Œ์„ ๋ณ€ํ™˜ํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ €์žฅํ•˜๋Š” ํƒ€์ž…

 

์ด์ œ ์กฐ๊ธˆ ๋” ์ต์ˆ™ํ•˜๊ฒŒ Record๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์„ ๊ฑฐ ๊ฐ™๋‹ค.