I'm using typescript, but my code has loads of errors.
The errors are as follow:
[var] is possibly null
(e.g. client.user is possible null
)
Property 'user' does not exist on type '[property X] | [property Y]'.
Property 'user' does not exist on type '[property Y]'.
(e.g. Property 'user' does not exist on type 'GuildMember | APIInteractionDataResolvedGuildMember'.
Property 'user' does not exist on type 'APIInteractionDataResolvedGuildMember'.
)
Argument of type '[type 1]' is not assignable to parameter of type '[type 2]'.
(e.g. Argument of type 'number' is not assignable to parameter of type 'string'.
)29 Replies
• What's your exact discord.js
npm list discord.js
and node node -v
version?
• Post the full error stack trace, not just the top part!
• Show your code!
• Explain what exactly your issue is.
• Not a discord.js issue? Check out #useful-servers.discord.js v14.9.0, node v18.15.0
it's only those kinds of errors for some reason
Yes, that's to be expected in TypeScript, the reasons is as it says, for example, client.user is possibly null as it can be that the client isn't ready, so you'd need to typeguard it by doing something like
if (client.isReady()) {}
, etc. That's how TypeScript works, not just for discord.js, but in generaloh
It's for the better though, it'd help you avoid errors later on causing your bot to crash, and give you those errors beforehand so things like that don't happen in production
I added "client.isReady()" and client.user is still possibly null
Can you show your code?
And where is this piece of code?
before client.on and almost right after client is created
And where are you logging in the client?
wdym
main function
Okay, now I fully understand, the client.on("ready,...) returns the ready client parameter, so you can just do client.on("ready", c () => console.log(
${client.user.tag}
) and this time it won't say that as the client will be ready
The client
in your case if from the constructor which may or may not be ready but the returned client from ready event will be
We're using c
there as you already have a variable with the name client, and also, you can use Events.ClientReady if you want to use Enumsso typing "client.on("ready", c () => console.log(`${client.user.tag} has logged in!`);`" instead of that
Yes, that should work
Cannot find name 'c'. apparently
',' expected. and Expected 2 arguments, but got 3. errors too
:/
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
Oops, overlooked that, yeah, remove the ()
I think the parenthesis after "client.on" is missing
k
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
same thing again, client.user is possibly null
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
oh ok
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
how would you fix the error
Parameter 'err' implicitly has an 'any' type.
?That's no longer discord.js-related, I'd suggest learning TypeScript, and #other-js-ts would be a better place.
The answer to your question is you shouldn't use any for the type as that's a very bad practice for TypeScript, there will always be a type that will work better so you should use that instead. Discord.js is very well typed and documented so you can easily know the types of different things