Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Type error when using socket.timeout.emitWithAck #4925

Closed
GStarP opened this issue Jan 18, 2024 · 4 comments
Closed

Type error when using socket.timeout.emitWithAck #4925

GStarP opened this issue Jan 18, 2024 · 4 comments
Labels
bug Something isn't working

Comments

@GStarP
Copy link

GStarP commented Jan 18, 2024

Describe the bug
When using socket.timeout.emitWithAck, even if you set EmitEvents correctly, the return type will be a wrong any.

To Reproduce

no need for server participation.

Server

Socket.IO client version: 4.7.4

Client

import type { Socket } from 'socket.io-client'

type MySocket = Socket<DefaultEventsMap, EmitEvents>
type DefaultEventsMap = {
  [event: string]: (...args: any[]) => void
}
type EmitEvents = {
  hello: (cb: (val: number) => void) => void
}

const socket = {} as MySocket
const r1 = socket.emitWithAck('hello') // Promise<number>
const r2 = socket.timeout(1000).emitWithAck('hello') // Promise<any>

Expected behavior
r2 should also be inferred as Promise<number>.

Platform:

  • Typescript version: 5.3.3

Additional context

The return type of emitWithAck is from Promise<FirstArg<Last<EventParams<EmitEvents, Ev>>>>, which picks the first argument of cb. However, timeout uses DecorateAcknowledgements then PrependTimeoutError, which prepend a Error type param to cb, so this FirstArg will focus on the error instead of the real return value.

Maybe replace FirstArg with a type LastArg<T> = T extends (...args: infer Args) => any ? Args extends [] ? never : Args extends [...infer _, infer Last] ? Last : never : never; will solve this issue? I'm not so familiar with the js logic, so just a hypothesis.

By the way, I have found the same problem in #4813 , it can be solved by using socket options ackTimeout in the client side (I have already adopted this solution). But I think it's better if this problem can be solved completely.

@GStarP GStarP added the to triage Waiting to be triaged by a member of the team label Jan 18, 2024
@darrachequesne
Copy link
Member

You are right, we need to apply the same changes on the client side: #4817

@darrachequesne darrachequesne added bug Something isn't working and removed to triage Waiting to be triaged by a member of the team labels Jan 22, 2024
@GStarP
Copy link
Author

GStarP commented Jan 22, 2024

Oh sorry, I should post this issue in the client side repo.

Thanks for answering, I will close this issue and look forward to the new version client.

@GStarP GStarP closed this as completed Jan 22, 2024
@DudaGod
Copy link

DudaGod commented Apr 11, 2024

You are right, we need to apply the same changes on the client side: #4817

Hello. Do I understand correctly that this is not supported on the client yet? When can I expect a new version?

Thanks.

@MiloWang2048
Copy link

I can reproduce this error with socket.io version 4.7.5, on server side, with or without timeout() call.

await targetUserSocket.emitWithAck("PrivateMessage", content);

Argument of type 'string' is not assignable to parameter of type 'never'.ts(2345)

But socket.emit() works just fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants