Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using template types in declarations #196

Open
bitomic opened this issue Feb 8, 2021 · 2 comments
Open

Using template types in declarations #196

bitomic opened this issue Feb 8, 2021 · 2 comments

Comments

@bitomic
Copy link

bitomic commented Feb 8, 2021

Most of the provided classes have a member to access to the AkairoClient, which commonly is a class that inherits from it. However, when using TypeScript, it complains if you try to access to your custom client's members because the client of, let's say, Command class is of type AkairoClient.

// client.ts
export default class CustomClient extends AkairoClient {
	private version: string
	constructor() {
		super({
			// akairo options
		}, {
			// discord.js options
		})
		this.version = '1.0.0'
	}
}

If I want to write a command like:

import { Command } from 'discord-akairo'
import { Message } from 'discord'
class VersionCommand extends Command {
	constructor() {
		super('version', {
			aliases: [ 'v' ]
		})
	}
	exec(message: Message) {
		return message.channel.send(`I am currently running on version ${this.client.version}`)
	}
}

You will receive the following warning:

Property 'version' does not exist on type 'AkairoClient'.ts(2339)

However, if you ignore it and run the bot, it will reply with the version properly. The problem is in the types definition.

A suggestion is to add template types to these classes, so you could:

import { Command } from 'discord-akairo'
import { Message } from 'discord'
import CustomClient from './client'
class VersionCommand extends Command<CustomClient> {
	constructor() {
		super('version', {
			aliases: [ 'v' ]
		})
	}
	exec(message: Message) {
		return message.channel.send(`I am currently running on version ${this.client.version}`)
	}
}

Where Command.client would not be of type AkairoClient but <Client extends AkairoClient>. Now I could access to my custom properties properly, without having to use // @ts-ignore.

Hopefully the point is clear enough!

@TymanWasTaken
Copy link

TymanWasTaken commented Feb 8, 2021

My solution to this was to just make a custom command class (extending Command) and override the client property of it to the correct type

@bitomic
Copy link
Author

bitomic commented Feb 8, 2021

They are pretty much equivalent, but I consider using a template type is a more elegant solution. But I never thought on that way, my current method was // @ts-ignore so I may use yours if they don't implement this/until they do.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants