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

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

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

Genericsはパラメトリックなポリモーフィズムの形態であり、.NET やJavaなど、様々な言語に実装されています。C++のテンプレートと同等の機能を持ち合わせています。

TypeScript

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

React.js

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

Q&A

0回答

659閲覧

[React+TypeScript] lazyを使ったHOCで、Propsの型を限定したい

maisumakun

総合スコア145184

Generics

Genericsはパラメトリックなポリモーフィズムの形態であり、.NET やJavaなど、様々な言語に実装されています。C++のテンプレートと同等の機能を持ち合わせています。

TypeScript

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

React.js

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

0グッド

2クリップ

投稿2023/01/08 08:55

前提

ReactをTypeScriptで書いていたのですが、lazyを使ったHOCについて、元のPropsの型を限定しようとしたら型エラーで回らなくなってしまいました。

実現したいこと

anyでごまかす以外にエラーを出さずに書く方法はないか

発生している問題・エラーメッセージ

# testの<LazyComponent>行にて Type 'P' is not assignable to type 'IntrinsicAttributes & ((PropsWithoutRef<P> & RefAttributes<Component<P, any, any>>) | PropsWithRef<P>)'. Type '{ show?: boolean | undefined; }' is not assignable to type 'IntrinsicAttributes & ((PropsWithoutRef<P> & RefAttributes<Component<P, any, any>>) | PropsWithRef<P>)'. Type 'P' is not assignable to type 'IntrinsicAttributes & PropsWithRef<P>'. Type '{ show?: boolean | undefined; }' is not assignable to type 'IntrinsicAttributes & PropsWithRef<P>'. Type '{ show?: boolean | undefined; }' is not assignable to type 'PropsWithRef<P>'. Type 'P' is not assignable to type 'PropsWithRef<P>'. Type '{ show?: boolean | undefined; }' is not assignable to type 'PropsWithRef<P>'. # test2の<LazyComponent>行にて Type 'P' is not assignable to type 'IntrinsicAttributes & (T extends MemoExoticComponent<infer U extends ComponentType<any>> | LazyExoticComponent<infer U extends ComponentType<...>> ? ReactManagedAttributes<...> : ReactManagedAttributes<...>)'. Type '{ show?: boolean | undefined; }' is not assignable to type 'IntrinsicAttributes & (T extends MemoExoticComponent<infer U extends ComponentType<any>> | LazyExoticComponent<infer U extends ComponentType<...>> ? ReactManagedAttributes<...> : ReactManagedAttributes<...>)'. Type '{ show?: boolean | undefined; }' is not assignable to type 'T extends MemoExoticComponent<infer U extends ComponentType<any>> | LazyExoticComponent<infer U extends ComponentType<any>> ? ReactManagedAttributes<...> : ReactManagedAttributes<...>'. Type 'P' is not assignable to type 'T extends MemoExoticComponent<infer U extends ComponentType<any>> | LazyExoticComponent<infer U extends ComponentType<any>> ? ReactManagedAttributes<...> : ReactManagedAttributes<...>'. Type '{ show?: boolean | undefined; }' is not assignable to type 'T extends MemoExoticComponent<infer U extends ComponentType<any>> | LazyExoticComponent<infer U extends ComponentType<any>> ? ReactManagedAttributes<...> : ReactManagedAttributes<...>'.

該当のソースコード

ts

1import React, { ComponentProps, ComponentType, lazy } from 'react'; 2 3const test = <P extends {show?: boolean}>( 4 promiseFunc: () => Promise<{default: ComponentType<P>}> 5) => { 6 const LazyComponent = lazy(promiseFunc); 7 const newComponent = (props: P) => { 8 return ( 9 <LazyComponent {...props} /> 10 ); 11 }; 12 return newComponent; 13}; 14 15const test2 = <P extends {show?: boolean}, T extends ComponentType<P>>( 16 promiseFunc: () => Promise<{default: T}> 17) => { 18 const LazyComponent = lazy(promiseFunc); 19 const newComponent = (props: ComponentProps<T>) => { 20 return ( 21 <LazyComponent {...props} /> 22 ); 23 }; 24 return newComponent; 25};

TypeScript Playground

試したこと

  • もともとはPropsの型を限定しないコードで、test2でジェネリクスが<T extends ComponentType<any>>だけのコードを書いていて、それは問題なく回っていました。
  • propsの型をPComponentProps<T>ComponentProps<typeof LazyComponent>のように切り替えてみましたが、エラーは改善しません。

補足情報(FW/ツールのバージョンなど)

  • React 17、18(どちらの型定義でも同様の問題に)
  • TypeScript 4.9.4

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

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

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

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

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

guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問