How to correctly type server action functions passed in to a child components as properties?
Example server action definition:
export const addProduct = async (product: Product): Promise<{ success: boolean } | null> => {
// do something
return {
success: true
};
}
Example page.tsx:
import { addProduct } from '@/actions/products-actions';
const ProductsAddPage = async () => {
return <>
<h1>Add Post</h1>
<section>
<ProductForm
action={addProduct}
actionType='add'
/>
</section>
</>;
}
export default ProductsAddPage;
And here is ProductForm definition:
'use client';
interface ProductFormProps {
action: any; // this is where I have no clue what to do?
actionType: string;
}
const ProductForm = (
{
action,
actionType
}: ProductFormProps
) => {
return <>
{/* show something */}
</>;
}
so I understand there are 'generics' where we can make the interface ProductFormProps
property action
instead of any
, a generic type so I can pass any type of server action function to it so it's not just a single type definition?