简体   繁体   English

我可以为 typescript 上的错误 ts(7053) 提供帮助吗?

[英]Can I have help for my error ts(7053) on typescript?

Hello so here I wanted to code a discord bot with discord.js (node.js) and extentions like opusscript and ytdl-core and I have as error if with this code.您好,所以在这里我想用 discord.js (node.js) 和 opusscript 和 ytdl-core 等扩展来编写 discord 机器人,如果使用此代码,我会遇到错误。 Thanks for your answer.感谢您的回答。

import * as Discord from "discord.js";
import {IBotCommand} from "../api";

var servers = {};

const ytdl = require("ytdl-core");

export default class play implements IBotCommand {

   private readonly _command = "play"

    help(): string {
        return "Cette commande ne fait absolument rien c'est juste pour le fun :)";
    }

    isThisCommand(command: string): boolean {
        return command === this._command;
    }

    async runCommand(args: string[], msgObject: Discord.Message, client:Discord.Client): Promise<void> {     

        function play(connection: Discord.VoiceConnection, msgObject: Discord.Message){
            var server = servers[msgObject.guild.id];


            server.dispatcher = connection.playStream(ytdl(server.queue[0], {filter: "audionly"}));

            server.queue.shift();

            server.dispatcher.on("end", function(){
                if(server.queue[0]){
                    play(connection, msgObject);
                } else {
                    connection.disconnect();
                }
            })
        }    

        if(!args[1]) {
            msgObject.channel.send("Vous devez donner un lien valide !");
            return;
        }

        if(!msgObject.member.voiceChannel) {
            msgObject.channel.send("Vous devez être dans un channel vocal pour jouer une musique !");
            return;
        }

        if(!servers[msgObject.guild.id]) servers[msgObject.guild.id] = {
            queue: []
        }

        var server = servers[msgObject.guild.id];

        server.queue.push(args[1]);

        if(!msgObject.guild.voiceConnection) msgObject.member.voiceChannel.join().then(function(connection){
            play(connection, msgObject);
        }) 
    }
}

error ts(7053)错误 ts(7053)

Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{}'.元素隐式具有“任何”类型,因为“字符串”类型的表达式不能用于索引类型“{}”。 No index signature with a parameter of type 'string' was found on type '{}'.在“{}”类型上找不到带有“字符串”类型参数的索引签名。

You'll need to add a type to servers .您需要向servers添加一个类型。 At the moment, typescript is taking its best guess, and it assumes it's an empty object.目前,typescript 正在做出最好的猜测,它假设它是一个空的 object。 You can use an index type , something like this (i don't exactly know what you want the Server type to look like, so feel free to make it more specific to meet your needs):您可以使用index type ,像这样(我不完全知道您希望 Server 类型看起来像什么,所以请随意使其更具体以满足您的需求):

interface Server {
  queue: any[]
}

var servers: { [key: string]: Server } = {};

I assume problem is with line:我认为问题出在行:

var server = servers[msgObject.guild.id];

TS is inferencing server as empty object type, and is right as you put there empty object. TS 将server推断为空 object 类型,并且在您将空 object 放在那里时是正确的。 It means that you cannot use any property of this object as there are no properties.这意味着您不能使用此 object 的任何属性,因为没有属性。 To fix this you need to at least define servers as some key->value map like要解决此问题,您至少需要将服务器定义为一些键->值 map

var servers: Record<string, any> = {} // any mean that value can be anything

But after this will work but you will have no type check in next lines, as you use server as a thing which has methods like dispatch .但是在这之后会起作用,但是您将不会在下一行中进行类型检查,因为您将 server 用作具有dispatch之类的方法的东西。 So to have it working properly on type level you need to define servers type in more specific way, so as I started from Record<string, any> you need to be more precise and define the Server type also and put it as Record<string, Server>因此,要让它在类型级别上正常工作,您需要以更具体的方式定义服务器类型,所以当我从Record<string, any>开始时,您需要更精确并定义服务器类型并将其作为Record<string, Server>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我该如何解决“错误:TypeScript 编译中缺少 \\src\\main.ts”。 - How can i solve "Error: \src\main.ts is missing from the TypeScript compilation." TS-2304错误 - 在“.ts”文件中导入“jquery”时,在TypeScript中找不到名称“Iterable” - TS-2304 Error - can not find name 'Iterable' in TypeScript while importing “jquery” in “.ts” file 我正在尝试将我的简单 typescript 应用程序部署到 firebase 云功能。 但我有这个错误 - I'm trying to deploy my simple typescript app to firebase cloud functions. But I have got this error TypeScript输入类型损坏(错误TS2687):&#39;iterator&#39;的所有声明必须具有相同的修饰符 - TypeScript typings broken (error TS2687): All declarations of 'iterator' must have identical modifiers TypeScript 构建错误 TS5023 - TypeScript build error TS5023 我在终端中有一个错误节点,你能帮我解决它吗 - I have an error Node in Terminal, can you help me please to fix it 我对 discord.js rpc 有错误? 有人可以帮帮我吗? - I have an error for discord.js rpc! Can someone please help me? 将我的打字稿代码发布到npm时如何处理d.ts文件 - How do I deal with d.ts files when publishing my typescript code to npm 无法运行我的 nodejs 和 typescript 项目,并且我得到“.ts 作为未知文件扩展名” - Cant run my nodejs and typescript project, and I am getting “.ts as an unknown file extension” 打字稿错误 TS1005: &#39;;&#39; 预期 - Typescript error TS1005: ';' expected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM