1
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?

TypeScript で無条件 throw による Unreachable code 判定を回避する

Posted at

たとえば、このようなコードがあるとする。

interface TestType {
  foo: number;
}

function test(data: TestType | null) {
  if (data === null) return 0;
  return data.foo;
}

デバッグやテストなどのため、test が呼ばれたらとりあえず throw したいとする。

interface TestType {
  foo: number;
}

function test(data: TestType | null) {
  throw "test"; // 追加
  if (data === null) return 0;
  return data.foo;
}

すると、以下のようなエラーが出てしまった。

Unreachable code detected.
'data' is possibly 'null'.

無条件で throw しているためその後のコードが実行されない、というのはわかる。
しかし、data === null のチェックをしているからその下の data.foodatanull にはならないはずなのに、これが null になりうるという間違った主張をされてしまっている。
迷惑である。

そこで、これを回避する方法を考えた。

試した結果、throw条件が必ず真になる ifの中に入れることで、Unreachable code 判定を回避しつつ、無条件で throw を実行できそうであることがわかった。

interface TestType {
  foo: number;
}

function test(data: TestType | null) {
  if (1) throw "test"; // 追加
  if (data === null) return 0;
  return data.foo;
}

これでエラーが出なくなった。
data.foodatanull になりうるという間違った主張もしてこなくなった。

if (true) throw "test";

では、エラーが出てしまった。

今回は、TS Playground の v5.6.2 で検証を行った。
もしも、将来のバージョンで条件 1 では恒真だとバレてしまい、エラーが出てしまった場合は、

if (!false) throw "test";
if (1 < 2) throw "test";
if (Math.sin(1) < 2) throw "test";

など、もう少し複雑な条件を試してみるといいかもしれない。(将来のバージョンなので、これらで本当にうまくいくかはわからないが)

1
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
1
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?