never
プログラミング言語の設計には、bottom型の概念があります。それは、データフロー解析を行うと現れるものです。TypeScriptはデータフロー解析(😎)を実行するので、決して起こりえないようなものを確実に表現する必要があります。
never
型は、このbottom型を表すためにTypeScriptで使用されます。自然発生するケース:- 絶対にreturnされない関数(例えば、関数本体に
while(true){}
がある場合) - 常にthrowする関数(例えば
function foo(){throw new Error('Not Implemented')}
の場合、foo
の戻り値の型はnever
です)
もちろん、このアノテーションを自分でも使用できます
let foo: never; // Okay
しかし、neverは、neverだけを代入することができます。例:
let foo: never = 123; // Error: Type number is not assignable to never
// Okay as the function's return type is `never`
let bar: never = (() => { throw new Error(`Throw my hands in the air like I just don't care`) })();
すばらしい。さあ、主な使用例を見てみましょう:)
たどり着けないコンテキスト で関数を呼び出すことはできません。
function foo(x: string | number): boolean {
if (typeof x === "string") {
return true;
} else if (typeof x === "number") {
return false;
}
// Without a never type we would error :
// - Not all code paths return a value (strict null checks)
// - Or Unreachable code detected
// But because TypeScript understands that `fail` function returns `never`
// It can allow you to call it as you might be using it for runtime safety / exhaustive checks.
return fail("Unexhaustive!");
}
function fail(message: string): never { throw new Error(message); }
関数が正常に終了することがないとき、
never
が返されると知ると、直感的にvoid
と同じように考えたくなるでしょう。しかし、void
は部品です。never
はうそつきです。何も返さない関数は
void
を返します。しかし、returnを返すことのない関数(または常にスローする)はnever
を返します。void
は(strictNullCheckingなしで)代入することができるものですが、never
はnever
以外のものに代入することはできません。最終更新 8mo ago