问题描述
我在使用 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 --');
。