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?

【React】input type numberから取得した値が文字列で「TS2345: Argument of type string is not assignable to parameter of type SetStateAction<number>」

1
Last updated at Posted at 2026-09-01

はじめに

問題

  • こんな感じでイベントハンドラからとった、target.valueがstringになり、number型のstateにセットするとtype errorで困った
<input type="number" id="time" name="time" value={time} onChange={(e: React.ChangeEvent<HTMLInputElement, HTMLInputElement>) => setTime(e.target.value)} />

解決

  • e.target.valueAsNumber で解決

終わりに

  • フロントでもできるだけoptionalチェインとか、Number() とか、 toString() とかは使わないで済むなら使いたくないなと感じた
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?