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
d.js toolkit
d.js toolkit2y ago
• 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.
samp
sampOP2y ago
discord.js v14.9.0, node v18.15.0 it's only those kinds of errors for some reason
Danial
Danial2y ago
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 general
samp
sampOP2y ago
oh
Danial
Danial2y ago
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
samp
sampOP2y ago
I added "client.isReady()" and client.user is still possibly null
Danial
Danial2y ago
Can you show your code?
samp
sampOP2y ago
if (client.isReady()) {
// log a logged in message into console
client.on('ready', () => console.log(`${client.user.tag} has logged in!`));
}
if (client.isReady()) {
// log a logged in message into console
client.on('ready', () => console.log(`${client.user.tag} has logged in!`));
}
Danial
Danial2y ago
And where is this piece of code?
samp
sampOP2y ago
samp
sampOP2y ago
before client.on and almost right after client is created
Danial
Danial2y ago
And where are you logging in the client?
samp
sampOP2y ago
wdym
d.js docs
d.js docs2y ago
method Client#login() Logs the client in, establishing a WebSocket connection to Discord.
samp
sampOP2y ago
main function
Danial
Danial2y ago
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 Enums
samp
sampOP2y ago
so typing "client.on("ready", c () => console.log(`${client.user.tag} has logged in!`);`" instead of that
Danial
Danial2y ago
Yes, that should work
samp
sampOP2y ago
Cannot find name 'c'. apparently ',' expected. and Expected 2 arguments, but got 3. errors too :/
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Danial
Danial2y ago
Oops, overlooked that, yeah, remove the ()
samp
sampOP2y ago
I think the parenthesis after "client.on" is missing k
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
samp
sampOP2y ago
same thing again, client.user is possibly null
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
samp
sampOP2y ago
oh ok
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
samp
sampOP2y ago
how would you fix the error Parameter 'err' implicitly has an 'any' type. ?
Danial
Danial2y ago
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
Want results from more Discord servers?
Add your server