# never 타입

## 설명 <a href="#desc" id="desc"></a>

`never`는 **일반적으로 함수의 리턴 타입으로 사용**됩니다. 함수의 리턴 타입으로 `never`가 사용될 경우, **항상 오류를 출력하거나 리턴 값을 절대로 내보내지 않음을 의미**합니다. 이는 무한 루프(loop)에 빠지는 것과 같습니다.

```typescript
// 항상 오류 발생
function invalid(message:string): never {
  throw new Error(message);
}

// never 타입을 결과 추론(Inferred)
function fail() {
  return invalid('실패');
}

// 무한 루프
function infiniteAnimate(): never {
  while ( true ) { infiniteAnimate(); }
}
```

`never` 타입을 지정한 변수에 `never`가 아닌 타입은 할당할 수 없습니다.

```typescript
let never_type:never;

// 오류 발생: 숫자 값을 never 타입 변수에 할당할 수 없습니다.
never_type = 99;

// 함수의 반환 값이 never 타입 이기 때문에 오류가 발생하지 않습니다.
never_type = (function():never { throw new Error('ERROR') })();
```

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

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

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

{% embed url="<https://www.typescriptlang.org/docs/handbook/basic-types.html#never>" %}
TypeScript - never 타입
{% endembed %}

TypeScript 공식 핸드북은 `never` 사용법에 대해 애매한 설명으로 일관합니다. 대신 TypeScript Deep Dive를 참고해 `never` 사용법을 정리하세요.

{% embed url="<https://basarat.gitbooks.io/typescript/docs/types/never.html>" %}
Never 타입
{% endembed %}


---

# 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://yamoo9.gitbook.io/typescript/types/never.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.
