#Creating a custom client class or creating custom attributes onto the client class in typescript
21 messages · Page 1 of 1 (latest)
The error comes from where you're instantiating the Client as a CustomClient, but the Client interface from Discord.js doesn't have the property yet. You can't assign anything to a variable of type CustomClient unless it implements all the properties from the start
You could write a function that instantiates a client, adds the required properties and returns the result as a CustomClient
Hey. Thanks for the response.
I'm not entirely sure what you mean by this.
By instantiating, this means initializing? So creating an instance of the client class?
I'm not sure how doing this inside of a function rather than outside (as seen in the code above) would allow me to fix the issue above. I'm not saying you are wrong, I believe you I'm just not sure how that works
In the function, put the client in a variable of type Client, then add your custom properties, then put the modified client into a variable of type CustomClient and return it (or just cast it upon return if you don't need another type check)
Essentially the problem is where you say const client: CustomClient = whatever is on the right has to implement the CustomClient interface right off the bat. So you need to put all of the required properties on the object before you assign it to a variable of that type
Hey. I'm sorry but I don't fully understand
function createClient(client: Client<boolean>) {
client.foobar = "hello"
}
In this example, client is stored in the client variable, and has type Client. But I cannot add the properties I want to add because foobar doesn't exist on type client.
That is me trying to do step one in your solution - Before I have tinkered with the CustomClient type
I have figured it out, or at least one way of doing it. Not sure if this is a bad way or not.
I created a new class which extends from client and I set the attributes which I want to specify in the constructor
export class CustomClient extends Client {
foobar: string
constructor(intents: any) {
super(intents);
this.foobar = "foobar"
}
}
I’m not trying to make a custom client (I don’t think) but I’m getting the Property 'commands' does not exist on type 'Client<boolean>' red squiggly line error.
I know I need to create a new class that extends discord.js's 'Client' but idk where/how to declare/export it and idk if I need to add anything to the index.js or whatever file I’m getting the errors in.
Mind helping? @jade pollen
I’ve seen people say to add this to a new .d.ts file
declare module "discord.js" {
export interface Client {
commands: Collection<unknown, any>
}
}
But I’m not sure where to put it because I did that and nothing changes
I get it for many properties. Not just commands.
I'm not sure about that, but you could use the same method as me to get what you desire. I'm not sure if theres a better way or not though.
export class CustomClient extends Client {
commands: any
constructor(intents: any) {
super(intents);
this.commands = []
}
}
export const client = new CustomClient({
intents: [...],
});
client.commands.push("Now you can access your commands client variable")

I’ll try it out
Thanks
The bot works fine... I just don’t like red squiggly lines...

Typescript is great, but it seems like a constant battle between good and bad 😄
Yeah I haven’t even begun to look at typescript.
I didn’t even looked at js until a month or two ago.