Frontend/TypeScript

[ ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ(TypeScript) ] object(key:value) type, interface ์„ ์–ธํ•˜๊ธฐ

YWTechIT 2021. 8. 31. 22:39
728x90

๐Ÿ“ object(key:value) type, interface ์„ ์–ธํ•˜๊ธฐ

TS๋ฅผ ์‚ฌ์šฉํ•˜๋‹ค๊ฐ€ string, number, boolean, array์™€ ๊ฐ™์€ ์ž๋ฃŒํ˜•์—๋Š” type์„ ์ž˜ ์„ ์–ธํ•  ์ˆ˜ ์žˆ์—ˆ๋Š”๋ฐ, ์œ ๋… key:value์™€ ๊ฐ™์€ object๋Š” type์„ ์–ด๋–ป๊ฒŒ ์„ ์–ธํ•ด์•ผ ํ•˜๋Š”์ง€ ํ—ท๊ฐˆ๋ ธ๋‹ค. ๋‚˜๋Š” ์ด๋•Œ๊นŒ์ง€ key:valueํ˜•ํƒœ์˜ object๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ ์–ธํ–ˆ๋‹ค.

 

interface PostType{
    sort: string;
}

const post: PostType = {sort: "asc"}
console.log(post);
๐Ÿ‘‰๐Ÿฝ { sort: 'asc' }

 

ReactQuery๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ useInfiniteQuery๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋„์ค‘ ํ•จ์ˆ˜ parameter๋กœ ๋ฐ˜ํ™˜๋˜๋Š” key:value ํ˜•ํƒœ์ธ post๊ฐ’์˜ key๋Š” type๋งŒ ์•Œ๊ณ  ๊ฐ’์€ ์ •ํ™•ํ•˜์ง€ ์•Š์„ ๋•Œ ํ˜น์€ post๊ฐ’์˜ value๋Š” type๋งŒ ์•Œ๊ณ  ๊ฐ’์€ ์ •ํ™•ํ•˜์ง€ ์•Š์„ ๋•Œ, ํ˜น์€ ๋น„๊ตฌ์กฐํ• ๋‹น๋ฌธ๋ฒ•(destructuring assignment)์„ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ๋Š” type์„ ์–ด๋–ป๊ฒŒ ์„ ์–ธํ•ด์•ผํ•˜๋Š”์ง€ ๊ถ๊ธˆํ–ˆ๋‹ค. ๊ทธ๋ž˜์„œ ๋ฐฐ์›Œ๋ณด์ž. how to declare object type!!

 

// post์˜ `key`๋Š” ์ž˜ ๋ชจ๋ฅด์ง€๋งŒ `type`์€ ์•Œ๊ณ  ์žˆ์„ ๋•Œ
type SortType = {[key in string]: string};
const post = {sort: "asc"};
const {sort} = post as SortType;

// post์˜ `key` ๊ฐ’์„ ๋ช‡ ๊ฐœ ์•Œ๊ณ  ์žˆ์„ ๋•Œ
type Keys = "sort" | "filter" 
type SortType = {[key in Keys]: string};
const post = {sort: "asc"};
const {sort} = post as SortType;

// post์˜ `value` ๊ฐ’์„ ๋ช‡ ๊ฐœ ์•Œ๊ณ  ์žˆ์„ ๋•Œ
type Values = "asc" | "desc" | "id";
type SortType = {[key in string]: Values};
const post = {sort: "asc"};
const {sort} = post as SortType;

// post์˜ `key:value`๋ฅผ ๋Œ€๋žต์ ์œผ๋กœ ์•Œ๊ณ  ์žˆ์„ ๋•Œ
type Keys = "sort" | "filter" 
type Values = "asc" | "desc" | "id";
type SortType = {[key in Keys]: Values};
const post = {sort: "asc"};
const {sort} = post as SortType;

// post์˜ ์ •ํ™•ํ•œ `key:value` ๋ชจ๋‘ ํ™•์‹คํ•œ ๊ฐ’์ผ ๋•Œ
type SortType = {sort: string};
const post = {sort: "asc"};
const {sort} = post as SortType;

// arrayํ˜•ํƒœ๋กœ return๋˜๋Š” post type ์„ ์–ธ
type PostObj = {[key in string]: string};
type PostType = Array<string | PostObj>
const post: PostType = ["usePosts", {sort: "asc"}];

 

728x90

 

์ง€๊ธˆ๊นŒ์ง€ ์ž‘์„ฑํ•œ ์ฝ”๋“œ๋ฅผ ํ† ๋Œ€๋กœ ์‚ดํŽด๋ณด๋ฉด ๋น„๊ตฌ์กฐํ• ๋‹น์˜ type์„ ์–ธ์€ ๋ณ€์ˆ˜ ์œ„์น˜๊ฐ€ ์•„๋‹Œ ํ• ๋‹นํ•˜๋Š” ๊ฐ’ ๋’ค์— asํ˜•ํƒœ๋กœ ๋ถ™์˜€๋‹ค. ์ด๋ฅผ ํ•œ๊ตญ๋ง๋กœ ๋‹ค์šด ์บ์ŠคํŒ… ํ˜น์€ ์˜์–ด๋กœ Type Assertions์ด๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค. ์ด์™€ ๊ด€๋ จํ•œ TypeScriptLang์— ๋‚˜์™€์žˆ๋Š” ์˜์–ด๋ฅผ ๋ฒˆ์—ญํ•˜๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๊ธ€์„ ์ฝ์„ ์ˆ˜ ์žˆ๋‹ค. ๋•Œ๋•Œ๋กœ TS๊ฐ€ ์•Œ ์ˆ˜ ์—†๋Š” ๊ฐ’์˜ ์œ ํ˜•์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. (...์ค‘๋žต...) ์ด๋Ÿฌํ•œ ์ƒํ™ฉ์—์„œ Type Assertions์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ณด๋‹ค ๊ตฌ์ฒด์ ์ธ ์œ ํ˜•์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

๋˜ํ•œ, TypeScript Deep Dive์—๋Š” TypeScript์—์„œ๋Š” ์‹œ์Šคํ…œ์ด ์ถ”๋ก  ๋ฐ ๋ถ„์„ํ•œ ํƒ€์ž… ๋‚ด์šฉ์„ ์šฐ๋ฆฌ๊ฐ€ ์›ํ•˜๋Š” ๋Œ€๋กœ ์–ผ๋งˆ๋“ ์ง€ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋•Œ "ํƒ€์ž… ํ‘œ๋ช…(type assertion)"์ด๋ผ ๋ถˆ๋ฆฌ๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์ด ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. TypeScript์˜ ํƒ€์ž… ํ‘œ๋ช…์€ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์ปดํŒŒ์ผ๋Ÿฌ์—๊ฒŒ ๋‚ด๊ฐ€ ๋„ˆ๋ณด๋‹ค ํƒ€์ž…์— ๋” ์ž˜ ์•Œ๊ณ  ์žˆ๊ณ , ๋‚˜์˜ ์ฃผ์žฅ์— ๋Œ€ํ•ด ์˜์‹ฌํ•˜์ง€ ๋ง๋ผ๊ณ  ํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

 

์ฆ‰, ๋‚ด๊ฐ€ ๋„ˆ๋ณด๋‹ค ํƒ€์ž…์„ ๋” ์ž˜ ์•Œ๊ณ ์žˆ์œผ๋‹ˆ๊นŒ, ๋‚ด ๋ง๋Œ€๋กœ ํ•ด!๊ฐ€ ๋œ๋‹ค. 

 

reference

  1. How to Specify Types for Destructured Object Properties Using TypeScript?
  2. typescriptlang
  3. TypeScript Deep Dive
๋ฐ˜์‘ํ˜•