【问题标题】:TypeScript ':' expected打字稿':'预期
【发布时间】:2021-05-17 04:38:21
【问题描述】:

我遇到了 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)
    }
}

【问题讨论】:

  • channel = client.channels.cache.get('-- censored --')? 为什么末尾有??你想要; 代替吗?

标签: javascript typescript tsc


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 2018-03-06
      • 2013-08-20
      • 1970-01-01
      • 2019-10-12
      • 2017-09-27
      • 2016-01-11
      相关资源
      最近更新 更多