TypeScript에서 객체 안의 객체

객체는 별도의 인터페이스로 설명되는 다른 객체를 포함할 수 있습니다. 예제를 통해 살펴보겠습니다.

도시 객체를 설명하는 인터페이스가 있다고 가정해 봅시다:

typescriptinterface City {
  name: string
}

사용자를 설명하는 인터페이스도 만들어 봅시다. 사용자에는 이름, 나이 및 객체인 도시가 있습니다:

typescriptinterface User {
  name: string,
  age: number,
  city: City
}

사용자 객체를 생성해 봅시다:

typescriptlet city: City = {name: 'london'};
let user: User = {name: 'john', age: 30, city: city};

도시에 대해 별도의 변수를 만들 필요는 없습니다:

typescriptlet user: User = {
  name: 'john',
  age: 30,
  city: {name: 'london'}
};
Задание 1

다음 객체가 주어졌습니다:

typescriptlet employee {
  name: 'andrew',
  potision: {
    name: 'programmer'
    salary: 1000,
  },
  addr: {
    country: 'belarus',
    city: 'minsk'
  }
};

이 객체의 구조를 설명하는 인터페이스를 만드세요. 중첩된 객체는 별도의 인터페이스로 분리하세요.

Задание 2

다음 객체가 주어졌습니다:

typescriptlet user: User = {
  name: 'john',
  age: 30,
  parents: {
    mother: {
      name: 'jane',
      age: 30,
      parents: null
    },
    father: {
      name: 'eric',
      age: 30,
      parents: null
    }
  }
}

이 객체의 구조를 설명하는 인터페이스를 만드세요. 중첩된 객체는 별도의 인터페이스로 분리하세요.