admin管理员组文章数量:1389750
I have these TypeScript interfaces, and i want to extend HandleSendMessage with HandleMessage, but the void makes it a problem.
The project is in React.js with TypeScript and Vite, and it's part of handling sending and receiving messages, eventually, to and from the server.
How do i do this correctly?
For the moment i have this:
export interface ChatMessage {
message: string;
sessionId: string;
type: MessageType;
}
export interface HandleSendMessage {
(
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>,
setInput: React.Dispatch<React.SetStateAction<string>>
): void;
}
export interface HandleReceiveMessage {
(
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>
): void;
}
The HandleSendMessage and HandleReceiveMessage have redundant code which i try to eliminate.
I tried to combine these into a HandleMessage and extend to that, but it's not working and i suspect the "void" is causing the problem, since i'm not entirely sure how to extend into this "void".
Here's the code i tried:
export interface ChatMessage {
message: string;
sessionId: string;
type: MessageType;
}
interface HandleMessage {
(
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>
): void;
}
export interface HandleSendMessage extends HandleMessage {
(
setInput: React.Dispatch<React.SetStateAction<string>>
): void;
}
export interface HandleReceiveMessage extends HandleMessage {}
I suspect it get's extended beneath the void instead of inside the void.
Maybe I have to define the void differently? Or maybe what i'm trying to do is simply not possible?
Tried go through docs, didn't find anything specific enough. Copilot is an idiot for this.
I have these TypeScript interfaces, and i want to extend HandleSendMessage with HandleMessage, but the void makes it a problem.
The project is in React.js with TypeScript and Vite, and it's part of handling sending and receiving messages, eventually, to and from the server.
How do i do this correctly?
For the moment i have this:
export interface ChatMessage {
message: string;
sessionId: string;
type: MessageType;
}
export interface HandleSendMessage {
(
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>,
setInput: React.Dispatch<React.SetStateAction<string>>
): void;
}
export interface HandleReceiveMessage {
(
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>
): void;
}
The HandleSendMessage and HandleReceiveMessage have redundant code which i try to eliminate.
I tried to combine these into a HandleMessage and extend to that, but it's not working and i suspect the "void" is causing the problem, since i'm not entirely sure how to extend into this "void".
Here's the code i tried:
export interface ChatMessage {
message: string;
sessionId: string;
type: MessageType;
}
interface HandleMessage {
(
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>
): void;
}
export interface HandleSendMessage extends HandleMessage {
(
setInput: React.Dispatch<React.SetStateAction<string>>
): void;
}
export interface HandleReceiveMessage extends HandleMessage {}
I suspect it get's extended beneath the void instead of inside the void.
Maybe I have to define the void differently? Or maybe what i'm trying to do is simply not possible?
Tried go through docs, didn't find anything specific enough. Copilot is an idiot for this.
Share Improve this question asked Mar 13 at 9:11 KaerKaer 11 bronze badge1 Answer
Reset to default 0I don't think it is possible to extend a function interface to add an argument, as you intent to do.
You can do it using types declarations, as in this answer: https://stackoverflow/a/52294058/18375304
Based on a comment by mosh-feu in the original answer, you could try:
type HandleMessage = (
chatMessage: ChatMessage,
setMessages: React.Dispatch<React.SetStateAction<ChatMessage[]>>
) => void;
type HandleSendMessage = HandleMessage extends (...a: any[]) => infer R ? (...a:[...U: Parameters<HandleMessage>, setInput: React.Dispatch<React.SetStateAction<string>>]) => R: never;
本文标签: typescriptHow to correctly extend an interface when using voidStack Overflow
版权声明:本文标题:typescript - How to correctly extend an interface when using void - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744711968a2621181.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论