broadcastEval() and Typescript
A bit at a loss here, trying to use .broadcastEval to run a function on all shards, for this function I would like to add two parameters as context, one of which is a guild object, but I'm getting these Typescript errors and I just can't seem to find how one is supposed to work with the .broadcastEval method and typescript. Pictures below show the problem.
Removing the types from the function leaves all the code inside with the values as implicitly :any which then causes a bunch of new errors. Do I really need to leave these as "any" and then check inside the function if they are the required type to make typescript be happy, or is there a correct way to do this all?
14 Replies
- What's your exact discord.js
npm list discord.js
and node node -v
version?
- Not a discord.js issue? Check out #other-js-ts.
- Consider reading #how-to-get-help to improve your question!
- Explain what exactly your issue is.
- Post the full error stack trace, not just the top part!
- Show your code!
- Issue solved? Press the button!Error here:
Removing types here will cause all the code inside to complain about implicit any type for guild.
It seems the
guild
argument is of unknown type here
And your typecast is erroring
Comes straight from the
guildCreate
event:Thonk:
Well it's the result of multiple tries, can't find a way to get broadcastEval to be happy unless I let guild be type unkown
Can you give me the definition of reportServerAmount so I can look into this
o nvm you did
If I change it to:
The error immediately disappears from broadcastEval, but then I get a
Binding element 'guild' implicitly has an 'any' type.ts(7031)
on the function definition
So its either Typescript complains about the function or about the use of it in broadcastEvalYeah I have no idea sorry, someone better with TS will need to help
Yeah, thanks for the attempt, my TS also seems to be insufficient, just can't believe I'm the first to get this, couldn't find anything though.
Oh, I think I know why. One sec
@TayDex yeah, got it. You can't send classes between processes - anything you send has to be JSON serialisable
Which technically Guilds are, but the type for
Guild#toJSON() => unknown
So what is it you need the whole Guild object for, or can you just send its ID as a string?
No type error if I do this:
Oh its all starting to make sense now, well its a few things like name, id, member count and maybe a few more, so I didnt want to pass each as its own context/parameter
Okay so as like a summary for anyone who finds this in the future and as a response to you:
That is indeed the problem, although the code you have sent still give me an error and I have to type it differently:
As I want multiple things of the guild object, I'm now using this approach: :galaxybrain:
So I guess I have to pass each element individually, maybe someone knows an easier way.
Take what I say with a grain of salt, had a break from coding for a few months and just started with sharding, but the above seems to accomplish what I need without erroring.
Anyways thank you a lot for the help ended a 2:30h search of pure confusion. Although i do wonder if the guild class can technically be Json serialized wouldnt the best fix be discord.js adding a proper type instead of unknown?
I suspect there's a reason why it's unknown but honestly I don't know what it is
I think it just inherits it from Base#toJSON
And we havent made the effort to manually type every toJSON method
The typescript rewrite should address it natively
:issue_open: #9432 in discordjs/discord.js by papsavas opened <t:1682014152:R>
Type currently unknown
toJSON()
methods