質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.46%
ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

Q&A

解決済

1回答

1116閲覧

typescriptの型チェック難しすぎる

pokemonta

総合スコア170

ログイン

ログインは、ユーザーがコンピューターシステムにアクセスするプロセスの事を呼びます。

TypeScript

TypeScriptは、マイクロソフトによって開発された フリーでオープンソースのプログラミング言語です。 TypeScriptは、JavaScriptの構文の拡張であるので、既存の JavaScriptのコードにわずかな修正を加えれば動作します。

React.js

Reactは、アプリケーションのインターフェースを構築するためのオープンソースJavaScriptライブラリです。

0グッド

0クリップ

投稿2021/10/02 13:58

あるサイトのコードをそのままコピーして
画面が表示されるかテストしています。  リンク

Next.js × Typescriptのログイン画面になります。

login.tsxにおいて、以下のメッセージが表示されましたので

Type error: Type 'null' is not assignable to type 'string'.

18 | this.state = {
19 | credentials: {

20 | email: null,

| ^

21 | password: null,
22 | },
23 | isLoading: false,

8~11行目を以下のように修正しましたが

credentials: { email: string | any password: string | any }

今度は、別のエラーが発生します

Type error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ email: any; password: any; }'.
No index signature with a parameter of type 'string' was found on type '{ email: any; password: any; }'.

27 | handleCredentialsChange = (e: React.ChangeEvent<HTMLInputElement>) => {
28 | let { credentials } = this.state

29 | credentials[e.target.name] = e.target.value

| ^

30 |
31 | this.setState({ credentials })
32 | }

どうすれば解消するのでしょうか

login

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

以下の修正1修正2の、どちらかうまく行きそうな方を採用する、ってことでどないでっか?

修正1

LoginStateは、

typescript

1interface LoginState { 2 credentials: { 3 email: string 4 password: string 5 } 6 isLoading: boolean 7}

のままにしておいて、this.state.credentials の email と password の初期値を空文字列にする。

diff

1 this.state = { 2 credentials: { 3- email: null, 4+ email: '', 5- password: null, 6+ password: '', 7 },

  

または、

修正2

this.state.credentials のほうは修正せずに

typescript

1 this.state = { 2 credentials: { 3 email: null, 4 password: null, 5 },

のままにしておいて、LoginState の email と password の型を、以下のように null をユニオンで追加

diff

1interface LoginState { 2 credentials: { 3- email: string 4+ email: string | null 5- password: string 6+ password: string | null 7 } 8 isLoading: boolean 9}

追記

今度は、別のエラーが発生します

のほうの対応が抜けとったんで、追記します〜

先に書いた、修正1 と 修正2 のうち、 修正2 のほうを採用して、email および password の型を string | null  にする方向で修正します。

まず、以下のように、LoginState から、email と password を持つ部分を Credentials として、個別のインターフェースとして切り出し、さらに、keyof CredentialsCredentialsProp という型エイリアスをつけておきます。

diff

1 interface LoginProps {} 2+ 3+interface Credentials { 4+ email: string | null 5+ password: string | null 6+} 7+ 8+type CredentialsProp = keyof Credentials; 9+ 10 interface LoginState { 11- credentials: { 12- email: string | any 13- password: string | any 14- } 15+ credentials: Credentials 16 isLoading: boolean 17 } 18 19

ほんで、エラーの発生している以下の部分で、e.target.name に対して asによる型アサーションを追加します。

diff

1 handleCredentialsChange = (e: React.ChangeEvent<HTMLInputElement>) => { 2 let { credentials } = this.state 3- credentials[e.target.name] = e.target.value 4+ credentials[e.target.name as CredentialsProp] = e.target.value 5 6 this.setState({ credentials }) 7 }

以上で質問にあるエラーは消えると思いますわ〜。

投稿2021/10/02 14:29

編集2021/10/03 05:37
退会済みユーザー

退会済みユーザー

総合スコア0

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

pokemonta

2021/10/03 04:19

修正1、修正2どちらを対応しても 以下のエラーを解消できませんでした Type error: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ email: string | null; password: string | null; }'. No index signature with a parameter of type 'string' was found on type '{ email: string | null; password: string | null; }'. 27 | handleCredentialsChange = (e: React.ChangeEvent<HTMLInputElement>) => { 28 | let { credentials } = this.state > 29 | credentials[e.target.name] = e.target.value | ^ 30 | 31 | this.setState({ credentials }) 32 | }
退会済みユーザー

退会済みユーザー

2021/10/03 05:39

回答に、上記エラーの対応を追記しました。
pokemonta

2021/10/03 06:08

いけました。どうゆうテクニックですか。これは~
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.46%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問