[英]Properties do not exist for data type
I am new to TypeScript and I am having two problems with data types.我是 TypeScript 的新手,我在数据类型方面遇到了两个问题。
This is my code:这是我的代码:
const say: object = {
name: "say",
aliases: [""],
description: "",
usage: "",
run: (client: object, msg: object, args: string) => {
if (!args[0]) return;
msg.channel.send(args.join(" "));
},
};
export default say;
The errors that come out are:出现的错误是:
Property 'channel' does not exist on type 'object'.
“对象”类型上不存在属性“通道”。
Property 'join' does not exist on type 'string'.
“字符串”类型上不存在属性“加入”。
I did a console.log (typeof msg)
and console.log (typeof args)
and indeed its data type is object
and string
.我做了一个
console.log (typeof msg)
和console.log (typeof args)
并且它的数据类型确实是object
和string
。
I'm looking for an alternative to change the data type to any
.我正在寻找将数据类型更改为
any
的替代方法。
I assume that channel
and message
are discord.js
types.我假设
channel
和message
是discord.js
类型。 So you can check discord.js types from here: https://github.com/discordjs/discord.js/tree/master/typings所以你可以从这里检查 discord.js 类型: https://github.com/discordjs/discord.js/tree/master/typings
and you can use Message type like this:你可以像这样使用消息类型:
import {Message }from "discord.js"
in the second question.在第二个问题。 join is not a property for string.
join 不是字符串的属性。 it's the property for arrays so probably args would be
string[]
它是 arrays 的属性,所以 args 可能是
string[]
If you are sure about the type of msg, then you can create your own type.如果您确定 msg 的类型,那么您可以创建自己的类型。
interface Msg {
channel: type here;
}
at the run, use this interface. (client: object, msg: Msg, args: string)
if you are not sure about its structure, then just say 'msg' there without any typedef.如果您不确定它的结构,那么只需在此处说“msg”而不使用任何 typedef。
do you mean args: string[]
.你的意思是
args: string[]
。 It doesn't make sense to apply a join on a string here.在这里对字符串应用连接是没有意义的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.