[TIL]231218_type vs interface

ใ…‡ใ…–ใ…ˆใ…ฃยท2023๋…„ 12์›” 18์ผ
2

Today I Learned

๋ชฉ๋ก ๋ณด๊ธฐ
45/93
post-thumbnail

๐Ÿ‘ฉ๐Ÿปโ€๐Ÿ’ป Today Learn

  • ์•Œ๊ณ ๋ฆฌ์ฆ˜ 1๋ฌธ์ œ
  • Typescript๊ณผ์ œ ๋งˆ๋ฌด๋ฆฌ ๋ฐ ์ œ์ถœ
  • Next.js๊ฐ•์˜

๐Ÿ” ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œํ’€์ด

๋ฌธ์ œ) ํ–‰๋ ฌ์˜ ๋ง์…ˆ

ํ–‰๋ ฌ์˜ ๋ง์…ˆ์€ ํ–‰๊ณผ ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ฐ™์€ ๋‘ ํ–‰๋ ฌ์˜ ๊ฐ™์€ ํ–‰, ๊ฐ™์€ ์—ด์˜ ๊ฐ’์„ ์„œ๋กœ ๋”ํ•œ ๊ฒฐ๊ณผ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค. 2๊ฐœ์˜ ํ–‰๋ ฌ arr1๊ณผ arr2๋ฅผ ์ž…๋ ฅ๋ฐ›์•„, ํ–‰๋ ฌ ๋ง์…ˆ์˜ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ํ•จ์ˆ˜, solution์„ ์™„์„ฑํ•ด์ฃผ์„ธ์š”.

ํ’€์ด

function solution(arr1, arr2) {
    let answer = [[]];
    
    for (let i=0; i<arr1.length; i++){
       answer[i] = [];
     for (let j=0; j<arr1[i].length; j++){
        answer[i].push(arr1[i][j] + arr2[i][j])
        }  
      }
    return answer
    }


// push() ๋ฉ”์„œ๋“œ๋Š” ๋ฐฐ์—ด์˜ ๋์— ํ•˜๋‚˜ ์ด์ƒ์˜ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ๋ฐฐ์—ด์˜ ์ƒˆ๋กœ์šด ๊ธธ์ด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// answer๋ฅผ [] ๋กœ ์„ ์–ธํ•˜๋ฉด ๋ฐฐ์—ด๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜ ๋” ๋งŒ๋“ค์–ด์•ผํ•จ..!

์ด ๋ฌธ์ œ๋Š” ์–ด๋–ป๊ฒŒ๋“  ํ˜ผ์ž ํ’€์–ด๋ณด๋ ค๊ณ  ํ•˜๋‹ค๊ฐ€ ๊ฒฐ๊ตญ ๊ฒ€์ƒ‰์„ ํ•˜๊ณ  ๋ง์•˜๋‹ค ใ… ใ… 

์šฐ์„  ๋ฐฐ์—ด์•ˆ์— ๋ฐฐ์—ด์ด๋ผ๋Š” ๊ฒƒ์ด ์–ด๋ ต๊ฒŒ ๋Š๊ปด์กŒ๊ณ  answer๋ฅผ ์ด์ค‘๋ฐฐ์—ด๋กœ ์„ ์–ธํ•ด๋†“์•˜๋Š”๋ฐ ์ด๊ฑธ ๊ฐ„๊ณผํ•˜๊ณ  ์ฒซ๋ฒˆ์งธ for๋ฌธ ๋‹ค์Œ์— let result = [] ๋ฅผ ๋˜ ๋„ฃ์€ ํ›„ push๋ฅผ ํ•œ๋ฒˆ ๋” ํ•ด์คฌ๋”๋‹ˆ ๋งจ ์•ž๋ถ€๋ถ„์— ๋นˆ๋ฐฐ์—ด์ด ๋ถ™์–ด์„œ ๋‚˜์™”์—ˆ๋‹ค.


๐Ÿ’ก ์•Œ๊ฒŒ๋œ ๋‚ด์šฉ

Typescript์—์„œ ๊ฐ์ฒดํƒ€์ž… ์ •์˜

- type

- interface

ํƒ€์ž…์Šคํฌ๋ฆฝํŠธ์—์„œ ํƒ€์ž…์„ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์ด 2๊ฐ€์ง€ ์žˆ๋Š”๋ฐ ์ด 2๊ฐ€์ง€๋Š” ๋น„์Šทํ•œ ์—ญํ• ์„ ํ•˜์ง€๋งŒ ์ฐจ์ด์ ์ด ์กด์žฌํ•œ๋‹ค.

(์ฐพ์•„๋ณด๊ณ  ๋“ค์€ ๋‚ด์šฉ์„ ์ ์–ด๋ณด๋ ค๊ณ  ํ•˜๋Š”๋ฐ ํ‹€๋ฆฐ ๋ถ€๋ถ„์ด ์žˆ์„ ๊ฒฝ์šฐ ๋Œ“๊ธ€์— ์ •์ •ํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•  ๊ฒƒ ๊ฐ™๋‹น :)

๊ณตํ†ต์ 


// ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•ด๋„ ๋œ๋‹ค๊ณ  ํ•œ๋‹ค.
type Person = {
 name: string;
 age: number;
}

interface Person = {
 name: string;
 age: number;
}

// ๋‘˜ ๋‹ค ์ถ”๊ฐ€ ์†์„ฑ๊ณผ ํ•จ๊ป˜ ํ• ๋‹นํ•˜๋ฉด ๋™์ผํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
const yeji: Person = {
 name: 'yeji',
 age: 20,
 email: 'yeahzzl@gmail.com'
};

// ์ด๋Ÿด ๊ฒฝ์šฐ Personํ˜•์‹์— email์ด ์—†๋‹ค๋Š” ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค

์ฐจ์ด์ 

interface

1. ํ™•์žฅ(์ƒ์†)ํ•˜๋Š” ๋ฒ•

  • extends ํ‚ค์›Œ๋“œ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
interface Person = {
 name: string;
 age: number;
}

interface Adult extends Person {
 job: string;
}
  
const yeji: Adult = {
 name: 'yeji',
 age: 20,
 job: 'designer'
}

2. ์„ ์–ธ์  ํ™•์žฅ

  • interface๋Š” ์„ ์–ธ์  ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•˜๋‹ค.
interface Person {
  name: string;
  age: number;
}

interface Person { 
  gender: string;
}

const yeji: Person = {
  name: 'yeji',
  age: 20,
  gender: 'female'
}

3. ๊ทธ ์™ธ ์š”์•ฝ

  • ๊ฐ์ฒด์— ๋Œ€ํ•œ ํƒ€์ž…์„ ์„ ์–ธํ•  ๋•Œ๋งŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ™•์žฅ ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์„ ์„ ์–ธํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์˜๋„์น˜ ์•Š๊ฒŒ ์•”๋ฌต์ ์œผ๋กœ merge๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค.
  • ์จ์•ผํ•˜๋Š” ์ฝ”๋“œ๊ฐ€ ๊ธธ์–ด์ง„๋‹ค.
  • ๋ถˆํ•„์š”ํ•œ ํ”„๋กœํผํ‹ฐ์— ๋Œ€ํ•œ ์ ‘๊ทผ์ด ์—ฌ๋ŸฌํŽ˜์ด์ง€์—์„œ ํ•œ๋ฒˆ์— ๋  ์ˆ˜ ์žˆ๋‹ค.

type

1. ํ™•์žฅ(์ƒ์†)ํ•˜๋Š” ๋ฒ•

  • & ๊ธฐํ˜ธ๋ฅผ ์ด์šฉํ•˜์—ฌ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
type Person = {
 name: string;
 age: number;
}

type Adult = Person & {
 job: string;
}
  
const yeji: Adult = {
 name: 'yeji',
 age: 20,
 job: 'designer'
}

2. ์„ ์–ธ์  ํ™•์žฅ

  • type์€ ์„ ์–ธ์  ํ™•์žฅ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
type Person {
  name: string;
  age: number;
}

type Person { 
  gender: string;
}

// Error๋ฐœ์ƒ

3. ๊ทธ ์™ธ ์š”์•ฝ

  • ๋ชจ๋“  ํƒ€์ž…์„ ์„ ์–ธํ•  ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ™•์žฅ ๋ถˆ๊ฐ€๋Šฅํ•œ ํƒ€์ž…์„ ์„ ์–ธํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ํ”„๋ก ํŠธ์—”๋“œ์—์„œ๋Š” ์ƒํƒœ๋‚˜ ์›์‹œ์ ์ธ ํƒ€์ž…์„ ์ค‘์‹ฌ์œผ๋กœ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— type์ด ๋” ๋งค๋ ฅ์ ์œผ๋กœ ๋Š๊ปด์งˆ ์ˆ˜ ์žˆ๋‹ค.

โœ๐Ÿป ํšŒ๊ณ 

typescript์ด๊ฒŒ ๋งž๋‚˜..? ํ•˜๊ณ  ์žˆ๋˜ ์™€์ค‘์— ์‹œ์ž‘๋œ next.js.. ์ •์‹ ์—†์ง€๋งŒ ์„ ํƒ๊ณผ ์ง‘์ค‘์„ ์ž˜ ํ•ด์„œ ๋ถ€์กฑํ•œ ๋ถ€๋ถ„์„ ์ฐจ๊ทผ์ฐจ๊ทผ ์ฑ„์›Œ๋„ฃ์–ด๋ด์•ผ๊ฒ ๋‹ค.
์ด๊ฒƒ ์ €๊ฒƒ ์ฐ๋จนํ•˜๋‹ค๊ฐ€ ๋‹ค ๋”ฐ๋ผ๊ฐ€๊ธฐ ๋ฒ„๊ฑฐ์›Œ ํ•˜๋Š” ์ƒํ™ฉ์ด ๊ฐ€์žฅ ์ตœ์•…์ผํ…Œ๋‹ˆ๊นŒ

profile
์›ฐ์”ฝํ‚น_๋‚˜๋Š” ๊ฒฝ์Ÿ๋ ฅ์„ ๊ฐ–์ถ˜ FE๊ฐœ๋ฐœ์ž๋กœ ์„ฑ์žฅํ•  ๊ฒƒ์ด๋‹ค.

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

comment-user-thumbnail
2023๋…„ 12์›” 19์ผ

์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ €๋ ‡๊ฒŒ ํ™•์žฅํ•  ์ˆ˜ ์žˆ๋Š”๊ฑด ์ฒ˜์Œ์•Œ์•˜๋„ค์š”!!

๋‹ต๊ธ€ ๋‹ฌ๊ธฐ