Cannot use `{data.length}` (Typescript)

i dont know the reason why {data.length} get error, it announced that 'data' is of type 'unknown'.ts(18046) Here is the code:
import fs from 'node:fs'
import path from 'node:path'

import { REST, Routes } from 'discord.js'
import { client } from '../src/index'
import * as dotenv from 'dotenv'

dotenv.config();

const rest = new REST().setToken(process.env.TOKEN!);
const commands = [];

const foldersPath = path.join(__dirname, '../commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders)
{
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

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());
}
}

(async () =>
{
try
{
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationCommands(client.user!.id),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// ^^^^
}
catch (error) { console.error(error); }
}
)();
import fs from 'node:fs'
import path from 'node:path'

import { REST, Routes } from 'discord.js'
import { client } from '../src/index'
import * as dotenv from 'dotenv'

dotenv.config();

const rest = new REST().setToken(process.env.TOKEN!);
const commands = [];

const foldersPath = path.join(__dirname, '../commands');
const commandFolders = fs.readdirSync(foldersPath);

for (const folder of commandFolders)
{
const commandsPath = path.join(foldersPath, folder);
const commandFiles = fs.readdirSync(commandsPath).filter(file => file.endsWith('.js'));

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());
}
}

(async () =>
{
try
{
console.log(`Started refreshing ${commands.length} application (/) commands.`);

const data = await rest.put(
Routes.applicationCommands(client.user!.id),
{ body: commands },
);

console.log(`Successfully reloaded ${data.length} application (/) commands.`);
// ^^^^
}
catch (error) { console.error(error); }
}
)();
6 Replies
d.js toolkit
d.js toolkit2y ago
• 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.
d4
d42y ago
REST#put has a return type of Promise<unknown>, you can type it with https://discord-api-types.dev/api/discord-api-types-v10#RESTPutAPIApplicationCommandsResult alternatively i believe using the API class from @discordjs/core/http-only can be a good choice https://discord.js.org/docs/packages/core/0.6.0/API:Class
harshfeudal
harshfeudalOP2y ago
well i decide to do this if possible:
const data: any[] = await rest.put(
Routes.applicationCommands(client.user!.id),
{ body: commands },
);
const data: any[] = await rest.put(
Routes.applicationCommands(client.user!.id),
{ body: commands },
);
so i think ... it can search for the lenght, will it?
d4
d42y ago
i would guess so, but you can also simply import RESTPutAPIApplicationCommandsResult from discord.js
harshfeudal
harshfeudalOP2y ago
alright Yes thanks ^^
d4
d42y ago
np
Want results from more Discord servers?
Add your server