TypeScriptでコンポーネントがpropsとして受け取る型を再現する
TSとRNでコンポーネントAを作っているとき、そのコンポーネントAに「FlatListに渡せるProps」をPropsとして渡せるように型を作りたかった。
ComponentPropsを使えば作りやすい。
import React, { ComponentProps } from "react"; type Props = { foo: string bar: number flatListProps: ComponentProps<typeof FlatList> } const A = ({ foo, bar, flatListProps }: Props) => { return <FlatList {...flatListProps} /> }
FlatListの中でも data, renderItem, getItemを除外した型を作りたい場合は
Omit<ComponentProps<typeof FlatList>, "data" | "renderItem" | "getItem">
でいけた