LoginSignup
0
0

More than 3 years have passed since last update.

TypeScriptで数値の空判定を行う

Posted at

経緯

TypeScriptで数値型のinput入力値の空判定をする必要がありました。
簡易的に再現すると下記のような感じで
valはnumberなので''との比較を行うことでエラーが発生しました。


const val: number = 0;
if (val === '') { // エラーになる
  // 処理
}

解決方法

数値型の値を文字列化して別の変数に代入することで
空判定が可能になりました。


const val: number = 0;
const valStr = String(val); // 空判定のため文字列化
if (valStr === '') { // エラーにならない
  // 処理
}

参考

JavaScript のデータ型とデータ構造
https://developer.mozilla.org/ja/docs/Web/JavaScript/Data_structures

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