πŸ“˜ TypeScript/κ°œλ…

[μ½”λ”©μ• ν”Œ] ν•¨μˆ˜μ™€ object λ©”μ„œλ“œμ— type alias μ§€μ •ν•˜κΈ°

Zoeeey 2023. 12. 13. 14:05

ν•¨μˆ˜ type alias

ν•¨μˆ˜ νƒ€μž…λ„ type alias둜 μ €μž₯ν•΄μ„œ μ“Έ 수 μžˆλ‹€.

type NumOut = (x : number, y : number ) => number 
let ABC :NumOut = function(x,y){
  return x + y
}

function ν‚€μ›Œλ“œμ—λŠ” νŒŒλΌλ―Έν„° λ‚΄λΆ€λ‚˜ {} λ°”λ‘œ μ™Όμͺ½μ—λ§Œ νƒ€μž…μ§€μ •μ΄ κ°€λŠ₯ν•˜κΈ° λ•Œλ¬Έμ— function ν•¨μˆ˜μ΄λ¦„ :NumOut (){} ν˜•νƒœλŠ” μ‚¬μš©ν•  수 μ—†μ–΄μ„œ μœ„ μ½”λ“œμ²˜λŸΌ μ‚¬μš©ν•œλ‹€.

*** μ£Όμ˜μ‚¬ν•­ : ν•¨μˆ˜ type aliasλ₯Ό λΆ€μ°©ν•˜λ €λ©΄ ν•¨μˆ˜ν‘œν˜„μ‹μœΌλ‘œ 써야 ν•œλ‹€.


object λ©”μ„œλ“œ type alias

type Member = {
  name : string,
  age : number,
  plusOne : ( x :number ) => number,
  changeName : () => void
}
let memberData :Member = {
  name : 'kim',
  age : 30,
  plusOne (x){
    return x + 1;
  },
  changeName : () => {
    console.log('μ•ˆλ…•');
  }
}
memberData.plusOne(1);
memberData.changeName();

좜처 : μ½”λ”©μ• ν”Œ https://codingapple.com/ / TypeScript Documentation https://www.typescriptlang.org/docs/handbook/2/objects.html