0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【TyeScript】never型の概要

Posted at

はじめに

この記事では、TypeScript の never型の概要を記載します。

never型とは

never型は、値を持たないことを示す型です。

「値を持たない」の具体例としては、以下のような例が挙げられます。

必ずエラーが発生する関数

sample.ts
function throwError(message: string): never {
  throw new Error(message);
}

終了しない関数

sample.ts
function infiniteLoop(): never {
  while (true) {
      // Infinite loop
  }
}

作り得ない型

sample.ts
type NumberString = number & string;

image.png

never型への代入 / never型の代入

never型にはnever型以外の値を代入することはできません。

image.png

image.png

image.png

一方、never型はどんな型にも代入できます。

image.png

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?