# 구조적 타이핑

## 구조적 타이핑

> 타입스크립트는 모든 속성이 동일하면 객체 타입의 이름이 다르더라도 동일한 타입으로 취급한다.

* 객체를 어떻게 만들었든 간에 구조가 같다면 동일한 객체로 인식한다는 것을 **구조적 타이핑**이라 한다.
* A 인터페이스의 구조를 모두 충족한다면 A 인터페이스라고 본다.
  * 인터페이스가 더 구체적일수록 해당 조건을 만족하기가 더 힘들다. 그래서 더 좁은 타입으로 본다.

```typescript
interface A {
  name: string
}

const otherObj = {
  name: 'woong';
  age: 30 // A 구조 외에 추가적인 속성이 정의되어있어도 A 구조만 충족하면 A로 본다.
}

const obj: A = otherObj; // A 인스턴스를 모두 충족하므로 대입 가능
```

### 동일한 구조 타입을 구분하려면?

> 서로를 구분할 수 있는 속성을 추가하여 구조적으로 동일하지 않게 만들면 된다.&#x20;

```typescript
interface Money {
  _type: 'money';
  amount: number;
  unit: string;
}

interface Liter {
  _type: 'liter';
  amount: number;
  unit: string;
}
```

* `_type`속성이 다르므로 서로 대입되지 않는다.&#x20;
* 객체를 구별할 수 있는 속성을 추가하여, 구조가 동일한 객체를 구분할 수 있다.
* 속성의 이름은 `_type`이 아닌 다른 이름을 사용해도 된다. 겹치지 않는 이름으로
* `_type` 과 같은 속성을 브랜드 속성이라 부른다.&#x20;
  * 브랜드 속성을 사용하는것을 브랜딩이라 부른다.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://taewoongs-organization.gitbook.io/jtwjs-dev-wiki/dev_note/typescript/undefined/undefined-1.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
