#[suggestion] changing the return type for events

65 messages · Page 1 of 1 (latest)

primal copper
#

I've only noticed the MessageCreate event but the return type for it is Promise<void>. I suggest changing it to Promise<any> because Promise<void> makes validating stuff and replying impossible. For example:

client.on("messageCreate", async Message => {
 let answer = Message.content;
 if (answer !== "cow") return await Message.channel.send("wrong answer"); // oops an error
})

Since I am returning a Promise<Message> i'll get a error saying that void and Message do not match. And that is why I suggest this, I do not want to use a more complicated to re-read validate statement

For example I can do this to go about the error:

client.on("messageCreate", async Message => {
 let answer = Message.content;
 if (answer !== "cow") {
  await Message.channel.send("wrong answer"); // oops an error
  return;
 }
})

but that is just unnecessary effort and unnecessary complication

vast topaz
#

Seems like a contradiction. Events shouldn't return anything because nothing will look at them

primal copper
vast topaz
#

And no it doesn't make validating and replying impossible. Simply do not return it

harsh ginkgo
#

i don't think the second is more complicated. it just follows the logical standard of returning

vast topaz
primal copper
harsh ginkgo
vast topaz
#

Or that^

primal copper
harsh ginkgo
#

yes that is indeed what i said

primal copper
#

what do u mean return void ...? u mean set the type?

vast topaz
#

void makes the expression void

primal copper
# harsh ginkgo yes that is indeed what i said

look, my event can have as many validations are it requires, it is easy to say that when theres only 1 or 2 lines of validation, but what if i had 10 or even 20. And what if something in my validation went wrong and i would need to reread a what could be a 10 line validation turned into 30 lines

harsh ginkgo
#

i have no idea what you are saying tbh

primal copper
harsh ginkgo
#

why do you want to include your message. including it has literally zero benefit

primal copper
# harsh ginkgo i have no idea what you are saying tbh

ill give u a visual example

what can be done:

if (...) return await message...;
if (...) return await message...;
if (...) return await message...;
if (...) return await message...;
if (...) return await message...;

what needs to be done:

if (...) {
 await message...;
 return;
}
if (...) {
 await message...;
 return;
}
if (...) {
 await message...;
 return;
}
if (...) {
 await message...;
 return;
}
if (...) {
 await message...;
 return;
}
harsh ginkgo
#

....or you just do else if

primal copper
harsh ginkgo
#

and even so, there is nothing wrong with doing it the second way

primal copper
#

visually speaking

harsh ginkgo
primal copper
harsh ginkgo
#

you could probably also do if (...) {await message...; return;}

#

all in one line

primal copper
#

especially when you're debugging and when you're frustrated

harsh ginkgo
harsh ginkgo
primal copper
harsh ginkgo
#

convenience is not the name of the game, type safety is.

peak flicker
#

Promise<any> is less safe for the implementation - its why void is preferred

primal copper
#

how does changing from void to any will make anything less safe

harsh ginkgo
#

don‘t use typescript or disable the typescript feature for return types if you are more concerned with convenience

peak flicker
#

that being said Promise<unknown> would be as safe and also solve your issue, probably.

primal copper
#

the return value is not being used

primal copper
harsh ginkgo
#

the point of typescript is being inconvenienced in favor of safer code :P

peak flicker
#

this is such an unproductive conversation

primal copper
peak flicker
#

then dont hold it

primal copper
#

because the other party is being unreasonable

peak flicker
#

ignore bad takes if they're repeatedly brought up

#

anyways, as I was pointing out

harsh ginkgo
peak flicker
#

I'm strongly against using any here because it's not quite correct for the internal implementation

#

but I believe unknown would be the correct middle ground

#

it allows you to return anything in the callback while the internal impl can't use the return value unsafely

primal copper
peak flicker
#

so much for ignoring them meguFace

primal copper
peak flicker
#

well, it shouldn't be is the thing

harsh ginkgo
#

wow just going to insult me then alright lol

peak flicker
#

thats why the type is void

#

it makes it impossible to use it

#

of course EE is implemented in plain JS so really it doesn't matter but its a type correctness thing meguFace

#

callbacks like this should always return a type that doesn't allow arbitrary usage

primal copper
#

well if changing to unkown allows me to validate more conveniently then i'd have no complains

peak flicker
#

I believe unknown should accomplish this, it just wasn't a thing when those types were written

primal copper
#

yea i tested it and unknown works

zealous rain
#

just do return void await Message.channel.send(…)

vast topaz
#

That was already suggested

charred tiger
#

Not going to change it