skip to Main Content

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


  1. TypeScript file cannot be run in Node.js directly using the node CLI. In order to do so, you can consider installing ts-node instead which can run TypeScript using ts-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:

    /** @type {string[]} */
    const commands = [];
    

    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)

    Login or Signup to reply.
  2. 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:

    tsc deploy-commands.ts
    

    or

    npx tsc deploy-commands.ts
    

    This will generate a deploy-commands.js file in the same directory.

    Run the compiled JavaScript file with Node.js:

    node deploy-commands.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:

    npm install ts-node --save-dev
    

    Then you can run your TypeScript file directly:

    npx ts-node deploy-commands.ts
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search