1. TypeScript - Tuple & Array πŸ‘©β€πŸ’»

1. Assignment

JavaScript μ—λŠ” Tuple 이 μ‘΄μž¬ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— TypeScript μ—μ„œλŠ” κ·Έλƒ₯ JavaScript Array κ°€ 볡수의 Types λ₯Ό 담을 수 μžˆλ‹€λŠ” 것을 μ΄μš©ν•΄ Tuple 둜 μ‚¬μš©ν•˜κΈ° λ•Œλ¬Έμ— Tuple κ³Ό Array λŠ” λ™μΌν•˜κ²Œ μ‚¬μš©μ΄ κ°€λŠ₯ν•˜λ‹€.

const [foo, bar] = ["λ‚˜λ¬΄", "ν˜Έκ·Έμ™€νŠΈ"]

console.log(foo)  // λ‚˜λ¬΄
console.log(bar)  // ν˜Έκ·Έμ™€νŠΈ

μœ„ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

const foo = "λ‚˜λ¬΄"
const bar = "ν˜Έκ·Έμ™€νŠΈ"

console.log(foo)  // λ‚˜λ¬΄
console.log(bar)  // ν˜Έκ·Έμ™€νŠΈ

λ¬Όλ‘  μ΄λ ‡κ²Œ μ‚¬μš©ν•  μˆ˜λ„ μžˆλ‹€.

const some = ["λ‚˜λ¬΄", "ν˜Έκ·Έμ™€νŠΈ"]
const [foo, bar] = some

console.log(foo)  // λ‚˜λ¬΄
console.log(bar)  // ν˜Έκ·Έμ™€νŠΈ

λ˜ν•œ default value λ₯Ό 지정할 수 μžˆλ‹€.

const [foo = "ν˜Έλ‘", bar = "사과"] = ["λ‚˜λ¬΄"]

console.log(foo)  // λ‚˜λ¬΄
console.log(bar)  // 사과

2. XOR Swap

JavaScript 의 Array λ₯Ό μ΄μš©ν•œ XOR Swap 을 μ§€μ›ν•˜λ―€λ‘œ μ•„λž˜μ™€ 같이 μ‰½κ²Œ μ²˜λ¦¬ν•  수 μžˆλ‹€

let [foo, bar] = ["λ‚˜λ¬΄", "ν˜Έκ·Έμ™€νŠΈ"]
;[foo, bar] = [bar, foo]

console.log(foo)  // ν˜Έκ·Έμ™€νŠΈ
console.log(bar)  // λ‚˜λ¬΄

;[foo, bar] = [bar, foo] 에 μ£Όλͺ©ν•˜μž. IIFE와 λ§ˆμ°¬κ°€μ§€λ‘œ μ•ˆμ „ν•œ μ½”λ“œλ₯Ό μœ„ν•΄ μ‹œμž‘ 뢀뢄에 ;λ₯Ό λ„£μ–΄μ£Όμž.

3. Return Types

사싀 Tuple μžμ²΄κ°€ Array μ΄λ―€λ‘œ μ•„λž˜μ™€ 같이 λ°”λ‘œ λΆ„ν•΄ν•΄ ν• λ‹Ήμ‹œν‚¬ 수 μžˆλ‹€.

const [first, last] = "Harry Potter".split(" ")

console.log(first)  // Harry
console.log(last)  // Potter

Assignment μ—μ„œ default value λ₯Ό μ •μ˜ν–ˆμ—ˆλ‹€. λ°˜λŒ€λ‘œ 일뢀 값을 λ¬΄μ‹œν•˜λŠ” 것 μ—­μ‹œ κ°€λŠ₯ν•˜λ‹€.

const foo = () => ["사과", "λ°°", "λ©”λ‘ ", "λ”ΈκΈ°"]

const [apple, , melon, strawberry] = foo()

console.log(apple, melon, strawberry) // 사과 λ©”λ‘ , λ”ΈκΈ°

, , λ₯Ό μ‚¬μš©ν•΄ 자리λ₯Ό λ§Œλ“€μ–΄ 주되 κ·Έλƒ₯ λ¬΄μ‹œν•˜λ©΄ λœλ‹€.

4. Spread Operator

const [fruits1, ...fruits2] = foo()

console.log(fruits1)  // 사과
console.log(fruits2)  // ["λ°°", "λ©”λ‘ ", "λ”ΈκΈ°"]

Spread Operator 와 ν•¨κ»˜ μ‚¬μš©ν•˜λŠ” 것 μ—­μ‹œ κ°€λŠ₯ν•˜λ‹€.


2. TypeScript - Object

1. Assignment

Destructuring 이 κ°€μž₯ 빛을 λ°œν•˜λŠ” 곳이 λ°”λ‘œ Object λ‹€.

const lecture1 = {
  subject: 'English',
  students: 40,
  level: 'Intermediate',
  discount: false
}

const { level, students } = lecture1

console.log(students) // 40
console.log(level)    // Intermediate

Object λŠ” Key-Value Types μ—¬μ„œ Array 와 달리 λΆˆν•„μš”ν•œ 값에 λŒ€ν•΄ , ,와 같이 빈 κ³΅κ°„μ΄λ‚˜ μˆœμ„œλ₯Ό κ³ λ €ν•  ν•„μš”λ„ 없이 자유둭게 λΆ„ν•΄κ°€ κ°€λŠ₯ν•˜λ‹€.

μ„ μ–Έκ³Ό 할당을 λΆ„λ¦¬ν•˜λŠ” 것 μ—­μ‹œ κ°€λŠ₯ν•˜λ‹€.

let students, level

;( { students, level } = lecture1 )

console.log(students) // 40
console.log(level)    // Intermediate

XOR Swap μ—μ„œ λ³Έ κ²ƒμ²˜λŸΌ 이 κ²½μš°λ„ IIFE 와 λ§ˆμ°¬κ°€μ§€λ‘œ μ‹œμž‘ 뢀뢄에 ;λ₯Ό λ„£μ–΄ μ£Όμ–΄μ•Ό ν•œλ‹€. 단, μœ„μ—μ„œλŠ”
;[foo, bar] = [bar, foo] μ΄λ ‡κ²Œλ§Œ ν•΄μ€˜λ„ μž‘λ™ν–ˆλ˜ 것과 달리 μ—¬κΈ°μ„œλŠ” { } 이 block 이 Code Chunk κ°€ μ•„λ‹Œ Object λΌλŠ” 것을 λͺ…μ‹œν•˜μ§€ μ•ŠμœΌλ©΄ μ½”λ“œ μ—λŸ¬κ°€ λ°œμƒν•œλ‹€.
λ”°λΌμ„œ ;( { students, level } = lecture1 )와 같이 ;( )둜 κ°μ‹Έμ€˜μ•Όν•œλ‹€.

2. Assignment with NewName

값을 ν• λ‹Ήν•  λ•Œ 뒀에 Types λ₯Ό μ •μ˜ν•˜λ“― : variableNameλ₯Ό λΆ™μ—¬μ£Όλ©΄ ν•΄λ‹Ή μ΄λ¦„μ˜ λ³€μˆ˜μ— ν• λ‹Ήλœλ‹€.

const { students, level: lectureLevel } = lecture1

console.log(students)     // 40
console.log(lectureLevel) // Intermediate

λ§ˆμ°¬κ°€μ§€λ‘œ default value λ₯Ό 지정할 수 μžˆλ‹€.

const { students = 30, expire = false } = lecture1

console.log(students) // 40
console.log(expire)   // false

3. Function Parameters

Destructuring 이 κ°€μž₯ μœ μš©ν•œ 곳이 λ°”λ‘œ ν•¨μˆ˜μ˜ Parameters λ‹€.

const draw = (width?: number, height?: number, color: string = 'white') => {
  if (width === undefined || height === undefined) return
  return `The color of this square is ${color}, and area is ${width * height}.`
}

console.log(draw(5, 7, 'blue'))
console.log(draw(3, 6))
The color of this square is blue, and area is 35.
The color of this square is white, and area is 18.

μœ„ ν•¨μˆ˜μ˜ 경우 Parameters 에 값을 각각 μ€˜μ•Όν•œλ‹€. λ¬Έμ œλŠ” μš°λ¦¬λŠ” λŒ€λΆ€λΆ„ μ €λŸ° μ‹μ˜ λ°μ΄ν„°λŠ” ν•˜λ‚˜μ˜ 객체둜 λ‹€λ£¬λ‹€λŠ” 것이닀. 즉, 넓이와 높이, 색상과 같은 μ •λ³΄λŠ” μ•„λž˜μ™€ 같이 μ‘΄μž¬ν•  것이닀.

const square1: Square = {
  width: 5,
  height: 7,
  color: 'blue'
}
const square2: Square = {
  width: 3,
  height: 6
}

λ”°λΌμ„œ μ΄λ ‡κ²Œ 직접 λΆ„ν•΄ν•΄μ„œ Arguments 둜 λ˜μ§€κ±°λ‚˜

draw(square1.width, square1.height, square1.color)

μ•„μ˜ˆ μ•„λž˜μ™€ 같이 ν•¨μˆ˜κ°€ Parameters 둜 Object λ₯Ό 받도둝 ν•΄μ•Όν•œλ‹€.

const draw = (square: Square) => {
  if (square.width === undefined || square.height === undefined) return
  return `The color of this square is ${square.color}, and area is ${square.width * square.height}.`
}

draw(square1)

μ–΄λ–€ 방법을 μ‚¬μš©ν•˜λ“  square.와 같은 μ‹μœΌλ‘œ Dot Syntax λ₯Ό μ‚¬μš©ν•΄μ•Όλ§Œ ν•˜κ³  μ½”λ“œκ°€ 길어진닀. 사싀 λŒ€λΆ€λΆ„ λ‹€λ₯Έ μ–Έμ–΄μ—μ„œλŠ” μ΄λ ‡κ²Œ 객체둜 닀룬닀. ν•˜μ§€λ§Œ TypeScript μ—μ„œλŠ” Destructuring 을 μ‚¬μš©ν•΄ 객체둜 λ‹€λ£¨λ©΄μ„œλ„ 객체가 μ•„λ‹Œ κ²ƒμ²˜λŸΌ 값을 μ‚¬μš©ν•  수 μžˆλ„λ‘ ν•¨μˆ˜λ₯Ό μ •μ˜ν•  수 μžˆλ‹€.

const draw = ({ width, height, color = 'white' }: Square) => {
  if (width === undefined || height === undefined) return
  return `The color of this square is ${color}, and area is ${width * height}.`
}

console.log(draw(square1))
console.log(draw(square2))
The color of this square is blue, and area is 35.
The color of this square is white, and area is 18.

Arguments 만 보면 draw(_:) ν•¨μˆ˜μ— 객체λ₯Ό 던져 λ„£μ–΄ μž‘λ™μ‹œν‚€μ§€λ§Œ, ν•¨μˆ˜ λ‚΄λΆ€μ—μ„œ λ°›λŠ” Parameters λŠ” Destructuring λ˜μ–΄ darw(_:_:_:) ν•¨μˆ˜μΈ κ²ƒμ²˜λŸΌ μž‘λ™ν•œλ‹€.

4. Nested Object

TypeScript 의 Destructuring 의 κ°•λ ₯ν•œ 점은 μ€‘μ²©λœ ꡬ쑰 μ—­μ‹œ μ‰½κ²Œ ν’€μ–΄λ‚Ό 수 μžˆλ‹€λŠ” 것이닀. 이것은 ν•¨μˆ˜μ˜ Parameters λ₯Ό ν’€μ–΄λ‚΄λŠ” 것 만큼 μœ μš©ν•˜λ‹€.

const styles = {
    size: {
        width: 300,
        height: 200
    },
    colors: {
        fontColor: 'red',
        bgColor: 'blue'
    }
}

쀑첩 ꡬ쑰λ₯Ό ν’€μ–΄λ‚΄λŠ” 방법은 Assignment with NewName 와 같이 : variableName을 μ΄μš©ν•˜λŠ”λ° μ΄λ•Œ 쀑첩 ꡬ쑰λ₯Ό ν‘œν˜„ν•˜κΈ° μœ„ν•΄ {originalProperty: variableName} ν˜•νƒœλ‘œ λ§Œλ“€μ–΄ μ£ΌλŠ” 것이닀. μ•„λž˜ 예제λ₯Ό 보자.

const { size: { width, height }, colors: { fontColor, bgColor } } = styles

console.log(width)      // 300
console.log(height)     // 200
console.log(fontColor)  // red
console.log(bgColor)    // blue

μ€‘μ²©λœ Object ꡬ쑰에 μ‘΄μž¬ν•˜λŠ” Properties λ₯Ό μ „λΆ€ 1μ°¨μ›μœΌλ‘œ λΆ„ν•΄μ‹œμΌ°λ‹€.


μ’€ 더 λ³΅μž‘ν•œ 예제λ₯Ό μ‚΄νŽ΄λ³΄μž.

const metadata = {
  title: "Scratchpad",
  translations: [
    {
      locale: "de",
      localizationTags: [],
      lastEdit: "2014-04-14T08:43:37",
      url: "/de/docs/Tools/Scratchpad",
      title: "JavaScript-Umgebung",
    },
  ],
  url: "/en-US/docs/Tools/Scratchpad",
}

metadata μ—μ„œ title, translations μ•ˆμͺ½μ˜ title 2개만 λΆ„ν•΄ν•˜κ³  μ‹Άλ‹€. 그런데 기쑴의 title 은 englishTitleμ΄λΌλŠ” λ³€μˆ˜λ‘œ, 그리고 translations μ•ˆμͺ½μ˜ title 은 localTitleμ΄λΌλŠ” λ³€μˆ˜λ‘œ λΆ„ν•΄ν•˜κ³ μž ν•œλ‹€.
λ”°λΌμ„œ translations μ•ˆμͺ½μ˜ title 은 [ ] μ•ˆμͺ½μ˜ { } μ•ˆμ— μžˆλ‹€. 즉, [ { } ] 에 μ ‘κ·Όν•΄μ•Όν•œλ‹€. 이 ꡬ쑰λ₯Ό κ·ΈλŒ€λ‘œ λ”°λΌκ°€λ³΄μž.

const {
  title: englishTitle,
  translations: [
    {
      title: localTitle
    }
  ]
} = metadata

console.log(englishTitle) // Scratchpad
console.log(localTitle)   // JavaScript-Umgebung

5. Iterating Over a Object in an Array

Object 의 쀑첩 λ³΄λ‹€λŠ” μ‰½μ§€λ§Œ, μ‹€μ œλ‘œ 더 많이 μ‚¬μš©λ˜λŠ” κ²½μš°λ‹€.

type Student = {
  name: string,
  age: number,
  grade: string
}

const studentInfo = [
  {name: 'David', age: 17, grade: 'junior'},
  {name: 'Julia', age: 18, grade: 'sophomore'},
  {name: 'Michael', age: 18, grade: 'sophomore'},
]
studentInfo.map((student, index)  => (
    console.log(`Student ${index + 1}: ${student.name}`)
))

Destructuring 이 μ—†λ‹€λ©΄ μ΄λ ‡κ²Œ μ ‘κ·Όν•˜κ² μ§€λ§Œ, Destructuring 을 μ‚¬μš©ν•˜λ©΄ μ•„λž˜μ™€ 같이 μ ‘κ·Όν•  수 μžˆλ‹€.

studentInfo.map(({name}, index)  => (
    console.log(`Student ${index + 1}: ${name}`)
))

3. Swift - Tuple πŸ‘©β€πŸ’»

1. Assignment

let (foo, bar) = ("λ‚˜λ¬΄", "ν˜Έκ·Έμ™€νŠΈ")

print(foo)  // λ‚˜λ¬΄
print(bar)  // ν˜Έκ·Έμ™€νŠΈ

μœ„ μ½”λ“œλŠ” λ‹€μŒκ³Ό κ°™λ‹€.

let foo = "λ‚˜λ¬΄"
let bar = "ν˜Έκ·Έμ™€νŠΈ"

print(foo)  // λ‚˜λ¬΄
print(bar)  // ν˜Έκ·Έμ™€νŠΈ

λ¬Όλ‘  μ΄λ ‡κ²Œ μ‚¬μš©ν•  μˆ˜λ„ μžˆλ‹€.

let some = ("λ‚˜λ¬΄", "ν˜Έκ·Έμ™€νŠΈ")
let (foo, bar) = some

print(foo)  // λ‚˜λ¬΄
print(bar)  // ν˜Έκ·Έμ™€νŠΈ

2. XOR Swap

Swift 의 Tuple 을 μ΄μš©ν•œ XOR Swap 을 μ§€μ›ν•˜λ―€λ‘œ μ•„λž˜μ™€ 같이 μ‰½κ²Œ μ²˜λ¦¬ν•  수 μžˆλ‹€.

var (foo, bar) = ("λ‚˜λ¬΄", "ν˜Έκ·Έμ™€νŠΈ")
(foo, bar) = (bar, foo)

print(foo)  // ν˜Έκ·Έμ™€νŠΈ
print(bar)  // λ‚˜λ¬΄

3. Return Types

λ˜ν•œ Array λ₯Ό Tuple 둜 λ°˜ν™˜ν•˜λŠ” ν•¨μˆ˜λ₯Ό λ§Œλ“€μ–΄ λ‹€μŒκ³Ό 같이 ν™œμš©ν•  수 μžˆλ‹€.

func splitName(_ name: String) -> (String, String) {
    let parts = name.components(separatedBy: " ")
    return (parts[0], parts[1])
}

let (first, last) = splitName("Harry Potter")

print(first)    // Harry
print(last)     // Potter

4. Swift - Dictionary

1. Iterating Over a Dictionary

// `Planet` Type
let Planet = [
    1: "Mercury",
    2: "Venus",
    3: "Earth",
    4: "Mars",
    5: "Jupiter",
    6: "Saturn",
    7: "Uranus",
    8: "Neptune"
]

Planet.sorted(by: <).forEach { (number, name) in
    print(#"\#(number)번째 μœ„μ„±μ€ "\#(name)" μž…λ‹ˆλ‹€."#)
}

2. Iterating Over a Dictionary in an Array

Array μžμ²΄λŠ” Destructuring ν•  것이 μ—†λ‹€. ν•˜μ§€λ§Œ Array μ•ˆμ— Dictionary κ°€ λ“€μ–΄μžˆλŠ” ν˜•νƒœλΌλ©΄, 이 Dictionary λ₯Ό Destructuring ν•  수 μžˆλ‹€.

// `[Planet]` Type
let Planet = [
    [1: "Mercury"],
    [2: "Venus"],
    [3: "Earth"],
    [4: "Mars"],
    [5: "Jupiter"],
    [6: "Saturn"],
    [7: "Uranus"],
    [8: "Neptune"]
]

Planet.flatMap { $0 }.forEach { (number, name) in
    print(#"\#(number)번째 μœ„μ„±μ€ "\#(name)" μž…λ‹ˆλ‹€."#)
}




Reference

  1. β€œDestructuring assignment.” MDN Web Docs. Apr. 18, 2023, MDN - Destructuring assignment.
  2. Paul Hudson. β€œWhat is destructuring?.” Hacking With Swift. May. 28, 2019, Swift Destructuring.