Ok so I get this error in TypeScript and it’s from line 8 I believe.
require('dotenv').config()
const { REST, Routes } = require('discord.js');
const fs = require('node:fs');
const path = require('node:path');
const clientId = process.env.clientId
const guildId = process.env.guildId
const token = process.env.token
const commands : string[] = [];
// Grab all the command files from the commands directory you created earlier
const foldersPath = path.join(__dirname, 'commands');
const commandFolders = fs.readdirSync(foldersPath);
for (const folder of commandFolders) {
// Grab all the command files from the commands directory you created earlier
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));
// Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment
for (const file of commandFiles) {
const filePath = path.join(commandsPath, file);
const command = require(filePath);
if ('data' in command && 'execute' in command) {
commands.push(command.data.toJSON());
} else {
console.log(`[WARNING] The command at ${filePath} is missing a required "data" or "execute" property.`);
}
}
}
// Construct and prepare an instance of the REST module
const rest = new REST().setToken(token);
// and deploy your commands!
(async () => {
try {
console.log(`Started refreshing ${commands.length} application (/) commands.`);
// The put method is used to fully refresh all commands in the guild with the current set
const data = await rest.put(
Routes.applicationGuildCommands(clientId, guildId),
{ body: commands },
);
console.log(`Successfully reloaded ${data.length} application (/) commands.`);
} catch (error) {
// And of course, make sure you catch and log any errors!
console.error(error);
}
})();
First of all the code was like const commands = []
but then TypeScript starts moaning at me giving me this error Argument of type 'any' is not assignable to parameter of type 'never'.
so I searched it up on stack overflow and it tells me to do what is in my code above. Now there are no initial errors but when I run node deploy-commands.ts
and it gives me the error above.
Been trying to solve this for a while.
To be clear, I do know that : is maybe not an identifier but if I put it back to = the code const commands = string[] = [];
doesn’t work.
2
Answers
TypeScript file cannot be run in Node.js directly using the
node
CLI. In order to do so, you can consider installingts-node
instead which can run TypeScript usingts-node deploy-commands.ts
in your terminal directly.In addition, the error is caused by your file extension (
.ts
), or because you have a// @ts-check
at the very top of your file. Try renaming it to.js
and/or remove that comment from the top so it will be treated as normal JS file and can be read by Node.js without TypeScript complaining about the error.If you still want to use
node
CLI, while utilising the typecheck by TypeScript, you can consider using JSDoc format instead like so:This will still be a valid JavaScript for Node.js to run while allowing TypeScript to parse the type information from JSDoc. (Reference: https://www.typescriptlang.org/docs/handbook/intro-to-js-ts.html)
You cannot directly run a TypeScript file using Node.js. You need to compile the TypeScript file to JavaScript first and then run the compiled JavaScript file with Node.js.
Make sure you have TypeScript installed globally or in your project.
Compile your deploy-commands.ts file to JavaScript using the TypeScript compiler (tsc). If you have TypeScript installed globally, you can run:
or
This will generate a deploy-commands.js file in the same directory.
Run the compiled JavaScript file with Node.js:
Alternatively, you can use ts-node to run TypeScript files directly without manually compiling them first. To use ts-node, you need to install it as a development dependency:
Then you can run your TypeScript file directly: