1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

型 'string | undefined' の引数を型 'SetStateAction' のパラメーターに割り当てることはできません。の解決方法について

Posted at

はじめに

下記のようなエラーが表示されました。

型 'string | undefined' の引数を型 'SetStateAction' のパラメーターに割り当てることはできません。
型 'undefined' を型 'SetStateAction' に割り当てることはできません。ts(2345)
"Qiita": Unknown word.cSpell
const matchedQiita: string | undefined

下記のように記述していました。

.ts
export class Users {
 public github_id?: string;
.tsx
 const [userGithub, setUserGithub] = useState<string | null>("");

setUserGithub(matchedGithub); // ユーザーのGithub管理ステートにその説明をセット

解決方法

nullをundefinedに変更

.tsx
 const [userGithub, setUserGithub] = useState<string | undefined>("");

setUserGithub(matchedGithub); // ユーザーのGithub管理ステートにその説明をセット

何気なく、stateにnullを記述していましたが、下記の資料を参考にnullとundefinedについて調べることにしました。

参考

おわりに

undefinedとnullも奥が深いですね。


1
1
1

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
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?