> For the complete documentation index, see [llms.txt](https://yamoo9.gitbook.io/typescript/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://yamoo9.gitbook.io/typescript/interface/optional-properties.md).

# 인터페이스 옵션 속성

인터페이스는 클래스와 흡사해 보이지만, 사용 목적이 다릅니다. 인터페이스는 객체 인스턴스를 생성 할 수 없으므로 타입 검사가 사용 목적이 됩니다. 인터페이스를 설정한 클래스는 인터페이스에 정의된, 즉 요구된 사항을 준수해야 합니다.

```typescript
interface ButtonInterface {
  onInit():void;
  onClick():void;
}

class ButtonComponent implements ButtonInterface {

  onInit() { console.log('버튼 컴포넌트 초기화') }
  onClick() { console.log('버튼 클릭') }

}
```

## 옵션 설정 <a href="#setting-option" id="setting-option"></a>

하지만 클래스는 **설정된 인터페이스에 정의된 속성 또는 메서드를 반드시 사용하지 않고, 필요에 따라 선택적으로 사용하고 싶을 수도 있을 겁니다. 이 경우 옵션(Optional) 속성 설정을 통해 사용자가 선택적으로 사용하게 설정**할 수 있습니다.

옵션 속성을 설정하는 방법은 간단합니다. **속성 이름 뒤에 `?`를 붙이면 옵션 속성**이 됩니다.

```typescript
interface ButtonInterface {
  // 속성 이름 뒤에 ? 기호가 붙으면 옵션 속성이 됩니다.
  onInit?():void;
  onClick():void;
}

class ButtonComponent implements ButtonInterface {

  // onInit 메서드가 설정되지 않아도 오류를 발생하지 않습니다.
  onClick() { console.log('버튼 클릭') }

}
```

## 실습 <a href="#practice" id="practice"></a>

{% embed url="<https://stackblitz.com/edit/ts-interface-option?embed=1&file=index.ts&view=editor>" %}

## 참고 <a href="#reference" id="reference"></a>

{% embed url="<https://www.typescriptlang.org/docs/handbook/interfaces.html#optional-properties>" %}
TypeScript - 인터페이스 옵션 설정
{% endembed %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://yamoo9.gitbook.io/typescript/interface/optional-properties.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
