구조분해 할당을 명시적으로 타이핑할 때는 다음과 같은 실수를 많이 합니다.

    const { prop: { nested: string } } = { 
      prop: { nested: 'hi' },
    };
    // const string: string
    console.log(nested); 
    // Cannot find name 'nested'.
    console.log(string);
    

    타이핑할 때 에러가 안 나서 올바르게 타이핑한 것이라 생각할 수 있지만 아닙니다. 앞의 코드는 nested의 타입을 string으로 표기한 것이 아니라 nested 속성 값을 string이라는 변수 이름에 대입한 것입니다. 올바르게 타이핑하려면 다음과 같이 해야 합니다.

    const { prop: { nested } }: { prop: { nested: string } } = {
      prop: { nested: 'hi' },
    };
    console.log(nested);
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.