Q&A
前提
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};
試したこと
- もともとはPropsの型を限定しないコードで、
test2
でジェネリクスが<T extends ComponentType<any>>
だけのコードを書いていて、それは問題なく回っていました。 props
の型をP
やComponentProps<T>
、ComponentProps<typeof LazyComponent>
のように切り替えてみましたが、エラーは改善しません。
補足情報(FW/ツールのバージョンなど)
- React 17、18(どちらの型定義でも同様の問題に)
- TypeScript 4.9.4
あなたの回答
tips
プレビュー