TypeScript ':' expected

I'm having a problem with TypeScript compiler 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)
    }
}

2 answers

  • answered 2021-05-17 04:52 MMD

    the problem was with a ? as mentioned in the comment. 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);
        }
    };
    

  • answered 2021-05-17 04:56 prashant patel

    At the end of line channel = client.channels.cache.get('-- censored --')? due to ? compiler 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 --');.