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 badge
Add a comment  | 

1 Answer 1

Reset to default 0

I 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