-
Notifications
You must be signed in to change notification settings - Fork 3k
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
When emitting from timeout() prepend timeout error #1570
Conversation
I noticed an issue engina#1 Also fixed with the help of systemfault from irc.libera.chat/typescript |
When emitting with a timeout (added in version 4.4.0), the "err" argument was not properly typed and would require to split the client and server typings. It will now be automatically inferred as an Error object. Workaround for previous versions: ```ts type WithTimeoutAck<isEmitter extends boolean, args extends any[]> = isEmitter extends true ? [Error, ...args] : args; interface ClientToServerEvents<isEmitter extends boolean = false> { withAck: (data: { argName: boolean }, callback: (...args: WithTimeoutAck<isEmitter, [string]>) => void) => void; } interface ServerToClientEvents<isEmitter extends boolean = false> { } const io = new Server<ClientToServerEvents, ServerToClientEvents<true>>(3000); io.on("connection", (socket) => { socket.on("withAck", (val, cb) => { cb("123"); }); }); const socket: Socket<ServerToClientEvents, ClientToServerEvents<true>> = ioc("http://localhost:3000"); socket.timeout(100).emit("withAck", { argName: true }, (err, val) => { // ... }); ``` Related: #1555
Merged as 33e4172. Thanks a lot ❤️ |
Please see engina#1 Merged solution is suboptimal -- it loses argument labels. Perfect typing solution is proposed in the link and forked repo -- but it requires a newer tsc it seems. I tried to clean up a pull request for that too, but I got lost in build system intricacies and gave up. |
@engina arf, it seems you are right: I tried to fix the behavior with an untyped socket (which defaults to If you have any pointer 😇 |
This follows [1], in order to keep the label of each argument. [1]: 33e4172 Related: - #1570 (comment) - microsoft/TypeScript#39941 - microsoft/TypeScript#48049
@darrachequesne This gives perfect typings: https://github.com/engina/socket.io-client/blob/c8ff4b6351ec575ecf3dbcd4c487aa3c5283795e/lib/socket.ts But your build system (tsd?) doesn't like it. So I started upgrading tsc (wrongfully i guess) but then things started to get hairy. And at this point I was exhausted :) I would like to look at that again but very busy with work now. My suggestion:
|
Attempts to fix #1555
The kind of change this PR does introduce
Current behaviour
New behaviour
Other information (e.g. related issues)