admin管理员组文章数量:1299996
/component/button.tsx
"client component"
export const DeleteButton = ({ id }: { id: string }) => {
const deleteImageWithId = deleteImage.bind(null, id);
return (
<form
action={deleteImageWithId}
className="py-3 text-sm bg-gray-50 rounded-br-md w-full hover:bg-gray-100 text-center"
>
<DeleteBtn />
</form>
);
};
/lib/actions.ts
"server component"
// Delete Image
export const deleteImage = async (id: string) => {
const data = await getImageById(id);
if (!data) return { message: "No data found" };
await del(data.image);
try {
await prisma.upload.delete({
where: { id },
});
} catch (error) {
return { message: "Failed to delete data" };
}
revalidatePath("/");
};
Error message:
./components/button.tsx:44:7 Type error: Type '() => Promise<{ message: string; } | undefined>' is not assignable to type 'string | ((formData: FormData) => void | Promise<void>) | undefined'. Type '() => Promise<{ message: string; } | undefined>' is not assignable to type '(formData: FormData) => void | Promise<void>'. Type 'Promise<{ message: string; } | undefined>' is not assignable to type 'void | Promise<void>'. Type 'Promise<{ message: string; } | undefined>' is not assignable to type 'void | Promise<void>'. Type 'Promise<{ message: string; } | undefined>' is not assignable to type 'Promise<void>'. Type '{ message: string; } | undefined' is not assignable to type 'void'. Type '{ message: string; }' is not assignable to type 'void'.
/component/button.tsx
"client component"
export const DeleteButton = ({ id }: { id: string }) => {
const deleteImageWithId = deleteImage.bind(null, id);
return (
<form
action={deleteImageWithId}
className="py-3 text-sm bg-gray-50 rounded-br-md w-full hover:bg-gray-100 text-center"
>
<DeleteBtn />
</form>
);
};
/lib/actions.ts
"server component"
// Delete Image
export const deleteImage = async (id: string) => {
const data = await getImageById(id);
if (!data) return { message: "No data found" };
await del(data.image);
try {
await prisma.upload.delete({
where: { id },
});
} catch (error) {
return { message: "Failed to delete data" };
}
revalidatePath("/");
};
Error message:
Share Improve this question edited Feb 11 at 15:15 Yakk - Adam Nevraumont 276k30 gold badges354 silver badges560 bronze badges asked Feb 11 at 14:59 eko prameko pram 654 bronze badges 0./components/button.tsx:44:7 Type error: Type '() => Promise<{ message: string; } | undefined>' is not assignable to type 'string | ((formData: FormData) => void | Promise<void>) | undefined'. Type '() => Promise<{ message: string; } | undefined>' is not assignable to type '(formData: FormData) => void | Promise<void>'. Type 'Promise<{ message: string; } | undefined>' is not assignable to type 'void | Promise<void>'. Type 'Promise<{ message: string; } | undefined>' is not assignable to type 'void | Promise<void>'. Type 'Promise<{ message: string; } | undefined>' is not assignable to type 'Promise<void>'. Type '{ message: string; } | undefined' is not assignable to type 'void'. Type '{ message: string; }' is not assignable to type 'void'.
1 Answer
Reset to default 1() => Promise<{ message: string; } | undefined>
is the type of deleteImageWithId
, aka deleteImage.bind(null,id)
. You take no arguments ()
and return a promise of either an object containing a field message
which is a string, or something undefined.
You are trying to put that object somewhere that expects an object of type string | ((formData: FormData) => void | Promise) | undefined
.
This is probably action
; as the action of a form, it is passed the form data. Your form is pretty empty.
Probably the easiest way to deal with this would be to create a lambda wrapper that takes a FormData
and throws it away.
const deleteImageWithId = async (discard: FormData)=>{
/* await maybe? */ deleteImage(id);
};
Something like that should fix up the type of deleteImageWithId
so it matches one of the expected signatures of action
.
Failing the above working, you may have to modify deleteImage
to not return { message: "whatever" }
that isn't being consumed by the code using it, or write a wrapper that uses that to do something.
本文标签:
版权声明:本文标题:next.js - Getting typescript error Type error: Type '() => Promise<{ message: string; } | undefined> 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741654765a2390693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论