πŸ“’[TypeScript λ§ˆμŠ€ν„° with Webpack & React] Step03.객체 νƒ€μž…

κΆŒμš©μ€€Β·2023λ…„ 12μ›” 6일
0
post-thumbnail

1.객체 νƒ€μž…

TypeScriptμ—μ„œλŠ” 객체λ₯Ό μ„€λͺ…ν•˜λŠ” μ–΄λ…Έν…Œμ΄μ…˜μ„ λ§Œλ“€ 수 있으며 λ³€μˆ˜ λ˜λŠ” 객체λ₯Ό μ„ μ–Έν•  λ•Œ, νƒ€μž…μ„ μ •μ˜ν•΄μ•Όν•œλ‹€.

μ •μ˜ν•˜λŠ” 방법은 interface와 type이 쑴재
일반적으둜 ν™•μž₯성이 쒋은 interface μ‚¬μš©μ„ ꢌμž₯ν•œλ‹€.
νƒ€μž…μ΄ μ‹¬ν”Œν•˜κ±°λ‚˜ μ ˆλŒ€ λ³€κ²½λ˜μ§€ μ•ŠλŠ” κ²½μš°μ—λŠ” type을 μ‚¬μš©ν•˜μ—¬ 객체의 νƒ€μž…μ„ μ •μ˜ν•œλ‹€.

interface κ΅¬ν˜„


interface IUser {
  name: string,
  age : number
}

let obj: IUser = { name: 'NAME', age: 29 };
type κ΅¬ν˜„

type UserType = {
  name: string,
  age : number
}

let obj: UserType = { name: 'NAME', age: 29 };
μ–΄λ…Έν…Œμ΄μ…˜ κ΅¬ν˜„.
const printName = (name : { first : string, last : string}){
  return Name : `${first} ${last}`;
}

printName({firstt : 'Will', last :  'Ferrell'})

2. 객체 νƒ€μž… 더 μ•Œμ•„λ³΄κΈ°

λ°˜ν™˜ νƒ€μž… μ΄λ‚˜ λ³€μˆ˜μ—λ„ 같은 μž‘μ—…μ„ ν•  수 μžˆλ‹€.
let coordinate: {x: number, y: number} = {x : 34, y: 20};

function randomCoordinate(): {x: number, y: number}{
  return{ x: Math. };
}

객체 λ¦¬ν„°λŸ΄ ν˜•νƒœμ˜ λ°˜ν™˜ νƒ€μž… μ–΄λ…Έν…Œμ΄μ…˜μ„ κ°€μ§€λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€ 수 있고
객체 νƒ€μž…μ„ μ‚¬μš©ν•˜λŠ” λ³€μˆ˜λ₯Ό κ°€μ§ˆ 수 μžˆλ‹€.
ν•¨μˆ˜μ—λŒ€ν•œ νŒŒλΌλ―Έν„° νƒ€μž… μ• λ„ˆν…Œμ΄μ…˜μ„ μž‘μ„±ν•΄ 객체가 되게 ν•  수 μžˆλ‹€.

3. 초과 ν”„λ‘œνΌν‹°

직접 객체 λ¦¬ν„°λŸ΄μ„ μ „λ‹¬ν•˜λŠ” 경우 였λ₯˜λ₯Ό λ„μš°μ§€λ§Œ 사전에 λ³„λ„μ˜ λ³€μˆ˜λ‘œ μ •μ˜ν•˜λŠ” 과정을 거치면 μ €μž₯된 ν”„λ‘œνΌν‹° μ™Έμ—λŠ” κ·Έλƒ₯ λ¬΄μ‹œν•˜κ²Œλœλ‹€.

const printName = (name : { first : string, last : string}){
  return Name : `${first} ${last}`;
}

printName({first : 'Will', last :  'Ferrell' , age : 50}); // error

const person = {first : 'Will', last :  'Ferrell' , age : 50}

printName(person); // success μ •μ˜λœ first, last ν”„λ‘œνΌν‹° μ™Έμ—λŠ” λ¬΄μ‹œν•œλ‹€.

4. νƒ€μž… 별칭 μƒμ„±ν•˜κΈ°

νƒ€μž…μ„ μž¬μ‚¬μš©ν•˜κ³  이름을 μ €μž₯ν•˜λŠ” 방법이며 μ—΄ ν”„λ‘œνΌν‹°λ₯Ό κ°€μ§€λŠ” λ³΅μž‘ν•œ 객체 νƒ€μž…μ— μ‚¬μš©λœλ‹€.

type Point = {  
  x : number;
  y : number;
}

function doublePoint(point: Point): Point {
  return { x: point.x*2, y: point.y*2};
}

5. 쀑첩 객체

μ€‘μ²©λœ 객체인 경우 μ–΄λ…Έν…Œμ΄μ…˜ 처리λ₯Ό μ–΄λ–»κ²Œ ν• κΉŒ?
같은 ν˜•μ‹μœΌλ‘œ μ€‘μ²©ν•˜μ—¬ νƒ€μž…μ„ μ •μ˜ν•˜λ©΄ λœλ‹€.

type Song = {
  title: string;
  artist: string;
  numStreams: number;
  credits: { producer: string; writer: string};
};

const mySong: Song = {
  title : "Melody",
  artist : "eminem",
  numStreams : 123,
  credits: {
    producer : "Paul",
    writer : "Alex"
  }
}

6. 선택적 ν”„λ‘œνΌν‹°

객체와 객체 νƒ€μž… 섀정에 κ΄€ν•œ 것 쀑 선택적 ν”„λ‘œνΌν‹° 생성 방법도 μžˆλ‹€.
즉, 일뢀 ν”„λ‘œνΌν‹°λ₯Ό 선택적 μš”μ†Œλ‘œ λ§Œλ“€ 수 μžˆλ‹€.

ν”„λ‘œνΌν‹° 뒀에 ?λ₯Ό λΆ™μ΄λ©΄λœλ‹€.

type Point = {
  x: number;
  y: number;
  z?: number; // 선택적 ν”„λ‘œνΌν‹°
}

const myPoint: Point = {x: 1,y: 2}; // compile

7. readonly μ œμ–΄μž

객체 λ‚΄μ˜ νŠΉμ • ν”„λ‘œνΌν‹°λ₯Ό ν‘œμ‹œν•˜κ±°λ‚˜ λ°°μ—΄μ΄λ‚˜ ν΄λž˜μŠ€μ— μ ‘κ·Όν•  λ•Œ μ‚¬μš©λ˜λ©°

객체의 ν”„λ‘œνΌν‹°λ₯Ό readonly둜 ν‘œμ‹œν•˜λ©΄ TypeScriptμ—μ„œλŠ” ν”„λ‘œνΌν‹°λ₯Ό λ³€κ²½ν•  λ•Œ κ²½κ³ λ₯Ό ν•΄μ€€λ‹€.

type User = {
  readonly id: number;
  username: string;
};
  
const user: User = {
  id : 123,
  username : "catgurl"
}

console.log(user.id); // compile
user.id = 789; // error

8. ꡐ차 νƒ€μž…

2개 μ΄μƒμ˜ νƒ€μž…μ„ κ΅μ°¨ν•˜μ—¬ μ‚¬μš©ν•  수 μžˆλ‹€.

type Circle = {
  radius : number;
}

type Colorful = {
  color : string;
}

type ColorfulCircle = Circle & Colorful;

const happyFace: ColorfulCircle = {
  radius: 4,
  color: "yellow" 
};
profile
Brendan Eich, Jordan Walke, Evan You, κΆŒμš©μ€€

0개의 λŒ“κΈ€