콘텐츠로 이동

spread 연산자 in Object

Spread 연산자 in Object

관련 글: Nest js Create 테스트코드


질문 내용

https://github.dev/jmcdo29/testing-nestjs/tree/main/apps/typeorm-sample

  • catDTO
TypeScript
export interface CatDTO {  
  id?: string;  
  name?: string;  
  breed?: string;  
  age?: number;  
}  
  • mockingService
TypeScript
insertOne: jest  
  .fn()  
  .mockImplementation((cat: CatDTO) =>  
    Promise.resolve({ id: 'a uuid', ...cat }),  
  ),  

여기서 예제코드 보는데

TypeScript
describe("newCat", () => {  
  it("should create a new cat", async () => {  
    const newCatDTO: CatDTO = {  
      name: "New Cat 1",  
      breed: "New Breed 1",  
      age: 4,  
    };  
    await expect(controller.newCat(newCatDTO)).resolves.toEqual({  
      id: "a uuid",  
      ...newCatDTO,  
    });  
  });  
});  

이게 말이 되는 코드인가?

질문 관련 서치 내용

Spread syntax (…) - JavaScript | MDN
나머지 매개변수와 스프레드 문법

질문 답변 (해결 방안)

TypeScript
const obj1 = { name: "Tom", age: 30 };  
const obj2 = { name: "Alice" };  

// 👇️ const obj3: {name: string; age: number}  
const obj3 = { ...obj1, ...obj2 };  
console.log(obj3); // 👉️ {name: 'Alice', age: 30}  


마지막 업데이트 : 2025년 4월 23일
작성일 : 2023년 3월 13일