最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeScript ':' expected - Stack Overflow

programmeradmin2浏览0评论

I'm having a problem with TypeScript piler that expects a : in 17, 5 but I can't understand why and where to put it, my code:

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

I'm having a problem with TypeScript piler that expects a : in 17, 5 but I can't understand why and where to put it, my code:

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)
    }
}
Share Improve this question asked May 17, 2021 at 4:38 akkoboiakkoboi 391 gold badge1 silver badge6 bronze badges 1
  • 3 channel = client.channels.cache.get('-- censored --')? why is there a ? at the end? Did you want ; insteand? – Hao Wu Commented May 17, 2021 at 4:44
Add a ment  | 

2 Answers 2

Reset to default 2

the problem was with a ? as mentioned in the ment. also you forgot many semicolons in your code that I added.

I edited your hole code and here it is:

if you meant a null check by that ? then you had to place it somewhere else... see this below

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

At the end of line channel = client.channels.cache.get('-- censored --')? due to ? piler expect that it as a conditional (ternary) operator (condition ? exprIfTrue : exprIfFalse) so throw ':' expected error.

May be it's a typo and should be like channel = client.channels.cache.get('-- censored --');

Or it should be like channel = client.channels.cache?.get('-- censored --');.

发布评论

评论列表(0)

  1. 暂无评论