본문 바로가기
언어/TypeScript

spread operator

by seacode 2025. 4. 30.

여러 요소로 이루어진 배열, 객체, 함수에서 여러 개의 인자를 한방에 넣기 위한 역할을 한다.

말 그대로 여러 요소들을 spread 한다는 의미.

* 객체의 경우 같은 키 값을 가진다면 덮어쓰기를 하게된다.

const arr1 = [1,2,3]
const arr2 = [3,4,5]
const mergedArr = [...arr1, ...arr2] // [1,2,3,3,4,5]
const obj1 = { a:1, b:2 }
const obj2 = { a:2, c:3 }
const mergedObj = { ...obj1, ...obj2 } // { a:2, b:2, c:3 }
function test(x, y, z){
    return x + y + z;
}
const arr = [1, 2, 3, 4];
const result = test(...arr); // 함수 인자에 사용
console.log(result); // 6

 

 

'언어 > TypeScript' 카테고리의 다른 글

타입 가드  (0) 2025.04.17
Type Assertion (as 사용법)  (0) 2025.04.16
type vs interface  (0) 2025.04.16
타입 호환성  (0) 2025.04.15
타입 추론  (0) 2025.04.15