#[TypeScript] Non required option for slash commands

19 messages · Page 1 of 1 (latest)

bitter kestrel
#

Hello !

I wanted to know, I rewrite my bot with TypeScript and I have a problem. When I want to get the value of non-required option, my bot crash.

The concerned line (37) : let version = interaction.options.get('version', false).value as string;

Thanks in advance ^^

Mysterious_Dev

olive sedge
#

• 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.

umbral mural
#

you should use getString instead of .get.value and check if it's null

bitter kestrel
#

I don't have getString with typescript (4.9.5) :/

umbral mural
#

typeguard interaction

bitter kestrel
#

What ??

umbral mural
#

what kind of interaction is it? the class has a few typeguarding methods for each of them

bitter kestrel
#

slash command interaction

umbral mural
#

should be isChatinputCommandInteraction or something alike

bitter kestrel
#

Yes it is

umbral mural
#

if you already have a typeguard there you can use this type for the parameter of execute

#

btw you should enable strict in your tsconfig, then errors like the above would already be catched by tsc

bitter kestrel
#

So, else than set "strict" in tsconfig, do I need to do someting else to have the possibility to use getString ?

fickle glade
#

You need to set the parameter type of the function

umbral mural
#

strict is not for getString, it's to catch "cannot read of null" by ts

bitter kestrel
umbral mural
#

yes, as I've said above

#

why do you use non-null assertions here? your error clearly shows they can be null...