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

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

新規登録して質問してみよう
ただいま回答率
85.48%
Next.js

Next.jsは、Reactを用いたサーバサイドレンダリングなどを行う軽量なフレームワークです。Zeit社が開発しており、nextコマンドでプロジェクトを作成することにより、開発環境整備が整った環境が即時に作成できます。

TypeScript

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

Q&A

解決済

1回答

338閲覧

動的に型が変わるオブジェクトをpropsで渡したい

ta539tg70

総合スコア20

Next.js

Next.jsは、Reactを用いたサーバサイドレンダリングなどを行う軽量なフレームワークです。Zeit社が開発しており、nextコマンドでプロジェクトを作成することにより、開発環境整備が整った環境が即時に作成できます。

TypeScript

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

0グッド

0クリップ

投稿2022/08/05 00:52

Next.js + TypeScriptでシステムを作っています。
componentにpropsを渡す部分で Type 'CategoryDetail[]' is missing the following properties のエラーが出ているのでそれを消したいのですが、どのように解消したら良いでしょうか。

設計として少し特殊なのが、APIからのjsonレスポンスの型がカテゴリによって異なる点です。
具体的には、以下のように details の配列には Details が入るのですが、 Details の中の category_detailscategory_name によって Shirt だったり Bag だったりします。

json

1{ 2 "data": { 3 "id": 1, 4 "details": [ 5 { 6 "id": 1, 7 "category_name": "shirt", 8 "category_detail": { 9 "id": 1, 10 "size": "small", 11 "color": "white" 12 } 13 }, 14 { 15 "id": 2, 16 "category_name": "bag", 17 "category_detail": { 18 "id": 13, 19 "width": 30, 20 "height": 15 21 } 22 }, 23 { 24 "id": 3, 25 "category_name": "shirt", 26 "category_detail": { 27 "id": 45, 28 "size": "large", 29 "color": "pink" 30 } 31 } 32 ] 33 } 34}

typescript

1export interface Box { 2 id: number; 3 details: Details[]; 4} 5 6export interface Details { 7 id: number; 8 category_name: string; 9 category_detail: CategoryDetail[]; 10} 11 12export interface Shirt { 13 id: number; 14 size: string; 15 color: string; 16} 17 18export interface Bag { 19 id: number; 20 width: number; 21 height: number; 22} 23 24export type CategoryDetail = Shirt | Bag;

上記の前提で、問題のエラーが出ている箇所は以下です。

typescript

1const Foo: NextPage = () => { 2 const { box } = useFetchBox(); 3 4 return ( 5 <div> 6 {box.details.map((detail) => ( 7 {detail.category_name === "shirt" && <ShirtDetail categroy_detail={detail.category_detail} />} // error: Type 'CategoryDetail[]' is missing the following properties from type 'ShirtDetail': id, size, color 8 {detail.category_name === "bag" && <BagDetail categroy_detail={detail.category_detail} />} // error: Type 'CategoryDetail[]' is missing the following properties from type 'BagDetail': id, width, height 9 ))} 10 </div> 11 ) 12}

detail.category_name === "shirt" && のように指定した上で <ShirtDetail /> にpropsを渡そうとしているので動きそうなものなんですが、上記の通りlintエラーが出てしまいます。なにがいけないのでしょうか?( interface の定義方法がなにかしら誤っているのか...?)
自分では修正方法の見当がつかないため、アドバイスいただけますと幸いです。よろしくお願い致します。

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

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

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

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

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

hoshi-takanori

2022/08/05 01:42

category_detail は配列ではないのでは。 あとは tagged union を使えば…。
guest

回答1

0

ベストアンサー

detail.category_name === "shirt" && のように指定した上で <ShirtDetail /> にpropsを渡そうとしているので動きそうなものなんですが

category_nameの値とcategory_detailの関係性が型としては示されていませんので、detail.category_detailShirt | Bagのままであり、それ以上の型は決まりません。

いちばんシンプルには、categroy_detail={detail.category_detail as Shirt}のようにキャストしてください。

投稿2022/08/05 01:36

maisumakun

総合スコア145183

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

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

ta539tg70

2022/08/05 04:13 編集

ご回答ありがとうございます!なるほどです。 ご教示いただいた方法を試してみたところ、以下の状況になっています。 1. `categroy_detail={detail.category_detail as Shirt}` にしてみたところ `'Shirt' refers to a value, but is being used as a type here. Did you mean 'typeof Shirt'?` と新たに出た 2. サジェストの通り `category_detail={detail.category_detail as typeof Shirt}` にすると `Conversion of type 'CategoryDetail[]' to type 'VFC<ShirtDetailProps>' may be a mistake because neither type sufficiently overlaps with the other. If this was intentional, convert the expression to 'unknown' first.` と新たに出た 3. サジェストの通り `category_detail={detail.category_detail as unknown as typeof Shirt}` にすると `Type 'VoidFunctionComponent<ShirtDetailProps>' is missing the following properties from type 'Shirt': id, size, color` と新たに出た 4. 手詰まり状態 `ShirtDetail` の中はシンプルに以下のような感じです。 ```typescript interface ShirtDetailProps { category_detail: Shirt; } const ShirtDetail: React.VFC<ShirtDetailProps> = ({ category_detail, }) => { return ( // some code ); }; export default ShirtDetail; ``` こちらはどのように対処したら良いのでしょうか...。
ta539tg70

2022/08/05 04:14

コメントだとコードブロックが変換されないのですね...。見づらくて申し訳ないですが、お知恵をお借りできますと幸いです。
maisumakun

2022/08/05 05:06

> `'Shirt' refers to a value, but is being used as a type here. 型定義以外に変数としてのShirtが存在する状況ではありませんか?(名前を変えて両者を区別できるようにする必要があります)
ta539tg70

2022/08/05 06:50 編集

おおおお、おっしゃる通りでした!ありがとうございます! もうひとつのShirtの名前を変えたところ `category_detail={detail.category_detail as unknown as Shirt}` でエラーが消えるようになりました!!!本当にありがとうございました!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問