#Conditionnal type

1 messages · Page 1 of 1 (latest)

devout zodiac
#

use generics

#

conditionals like that won't work

tropic shore
devout zodiac
#

i mean, an example would be pretty much the entire thing

tropic shore
#
export abstract class DiscordSlashCommand<DataType> {
  /**
   *
   */
  readonly subcommands?:
    | Collection<string, DiscordSlashCommand<ApplicationCommandOptionData>>
    | undefined = undefined;
  /**
   *
   */
  abstract readonly data: DataType extends ApplicationCommandData
    ? ApplicationCommandData
    : ApplicationCommandOptionData;
#

I tried something like that but it's not exactly what I want

#

I'd like DataType to be optional for example

devout zodiac
#
export abstract class DiscordSlashCommand<T extends Collection<string, DiscordSlashCommand<null>> | null = Collection<string, DiscordSlashCommand<null>> | null> {
  abstract readonly subcommands: T
  abstract readonly data: T ...
}
tropic shore
devout zodiac
#

you can specify that as a conditional after T

tropic shore
#

I'm also trying to understand this thing 🤔

devout zodiac
#

or an interface would probably be better

#

wait no interface wouldn't work

#

!hb generics

crimson vineBOT
devout zodiac
#

btw ^

tropic shore
devout zodiac
#

T extends ... ? ... : ... kinda thing

#

but you'll need a few to handle all the appropriate cases

tropic shore
#

Thank you!

tropic shore
#

Looks like it's not working...

#
export abstract class DiscordSlashCommand<T extends Collection<string, DiscordSlashCommand<null>> | null = Collection<string, DiscordSlashCommand<null>> | null> {
  /**
   *
   */
  abstract readonly subcommands?: T;
  /**
   *
   */
  abstract readonly data: T extends Collection<string, DiscordSlashCommand>
    ? ApplicationCommandData
    : ApplicationCommandOptionData;
#

Maybe I could just extend the class to create a DiscordSlashSubcommand and override the type?