Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 对象[key]不是构造函数_Typescript - Fatal编程技术网

Typescript 对象[key]不是构造函数

Typescript 对象[key]不是构造函数,typescript,Typescript,我有一个命令,它实现了一个接口 我的命令 import iCommand from './i-command'; export default class Voice implements iCommand { args: String[]; message: any; client: any; config: any; constructor(args: String[], message: any) { this.args = []; thi

我有一个命令,它实现了一个接口

我的命令

import iCommand from './i-command';


export default class Voice implements iCommand {
  args: String[];    
  message: any;
  client: any;
  config: any;

  constructor(args: String[], message: any) {
    this.args = [];
    this.message = {};
    this.client = {};
    this.config = {};
  }

  test() {
    console.log('run');
  }

  setClient(client: any) {
    this.client = client;
  }

  setConfig(config: any) {
    this.config = config;
  }

  runCommand(): void {
    const emoji = this.message.guild.emojis.find(emoji => emoji.name === 'alpha_flag');
    this.message.channel.send('test').then(newMsg => {console.log(emoji); newMsg.react(emoji)});
    this.message.channel.send('test').then(newMsg => {console.log(emoji); newMsg.react(emoji)});
    this.client.channels.get(this.config.infoChannelId).send(`:fire: **Voice voting for player ${this.args[0]} started at** #voicing :fire:`);
    this.client.channels.get(this.config.voiceChannelId).send(`:fire: **Voting for player ${this.args[0]}** :fire:`).then(
      message => message.react(emoji)
    );
  }
}
我的界面:

export default interface iCommand {
  args: Array<String>;
  message: any;
  client: any;
  config: any;
  runCommand(): void;
  test(): void;
  setClient(client: any): void;
  setConfig(config: any): void;
}
和指挥官经理:

import {Commands} from './commands/commands';
import iCommand from './commands/i-command';

export default class CommandManager {
  client: any;
  config: any;

  constructor(config: any) {
    this.config = config;
    this.client = {};
  }

  setClient(client: any) {
    this.client = client;
  }

  getCommand(key: any, args: String[], message: any): void {
    let command: iCommand = new Commands[key](args, message);
    command.test();
     // @ts-ignore
    command.setConfig(this.config);
     // @ts-ignore
    command.setClient(this.client);
     // @ts-ignore
    return command;
  }
}

它是如何工作的?当用户使用命令时,例如
.voice
commandManager
使用按键返回命令。但是不管我做什么<代码>命令[key]()不是构造函数。你知道什么是有趣的。方法测试正在运行,但错误
typeError
禁用我的承诺。我试图禁用ts错误,但它也不起作用。。。我哪里有错误?我是否应该在key中使用
typeof

在CommandManager类中,getCommand方法的签名错误。钥匙有任何类型

 getCommand(key: any, args: String[], message: any): void {
这是错误的,因为键只能是“语音”。解决此问题的一种方法是将按键类型更改为“voice”(见下文)。该方法的返回类型也应该是iCommand,而不是void。Void表示该方法不返回任何值

getCommand(key: 'voice', args: String[], message: any): iCommand {
可以为键支持多个值。您可以定义包含所有可能键的新类型:

type CommandType = 'voice' | 'text';
getCommand看起来是这样的

getCommand(key: CommandType, args: String[], message: any): iCommand {

所以我改变了一切,就像你说的。但是你能解释一下命令类型吗?命令类型是字符串“voice”或字符串“text”。当然,您可以将其扩展到涵盖所有用例。但是,如果我是你,我会使用枚举
getCommand(key: CommandType, args: String[], message: any): iCommand {