微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

TypeScript ':' 预期

如何解决TypeScript ':' 预期

我在使用 TypeScript 编译器时遇到问题,需要在 17,5 中有 :,但我不明白为什么以及把它放在哪里,我的代码

import { Client,TextChannel } from "discord.js";

module.exports = {
    name: "ready",run: async(client: Client) => {
        client.user.setPresence({
            activities: [{
                name: "Aun en beta!"
            }],status: "idle"
        })

        let str = `-- censored --`
        let channel: TextChannel; 
        channel = client.channels.cache.get('-- censored --')?
        channel.send (str)
    }
}

解决方法

问题出在评论中提到的 ? 上。 您还忘记了我添加的代码中的许多分号

我编辑了你的漏洞代码,这里是:

如果您的意思是 ? 的空检查,那么您必须将它放在其他地方...请参阅下面的内容

import { Client,TextChannel } from "discord.js";

module.exports = {
    name: "ready",run: async (client: Client) => {
        client.user.setPresence({
            activities: [{
                name: "Aun en beta!"
            }],status: "idle"
        });

        let str = `-- censored --`;
        let channel: TextChannel;
        channel = client.channels.cache?.get('-- censored --');
//                                     ^^                     ^
        //                                            problem was here
        channel.send(str);
    }
};
,

在行的末尾 channel = client.channels.cache.get('-- censored --')? 由于 ? 编译器期望它作为条件(三元)运算符(condition ? exprIfTrue : exprIfFalse)所以抛出 ':' expected 错误。

可能是打字错误,应该像 channel = client.channels.cache.get('-- censored --');

或者它应该像 channel = client.channels.cache?.get('-- censored --');

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。