숫자 배열은 SimpleArr 객체 타입에 있는 모든 속성을 갖고 있습니다. 숫자 배열은 구조적으로 SimpleArr이라고 볼 수 있기에 대입할 수 있습니다.

    그럼 서로 대입하지 못하게 하려면 어떻게 해야 할까요? 어쩔 수 없이 서로를 구분하기 위한 속성을 추가해야 합니다. 다시 말해 구조적으로 동일하지 않게 만드는 것입니다.

    interface Money {
      __type: 'money';
      amount: number;
      unit: string;
    }
    
    interface Liter {
      __type: 'liter';
      amount: number;
      unit: string;
    }
    
    const liter: Liter = { amount: 1, unit: 'liter', __type: 'liter' };
    const circle: Money = liter;
    // Type 'Liter' is not assignable to type 'Money'. Types of property '__type' are incompatible. Type '"liter"' is not assignable to type '"money"'.
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.