在TypeScript中添加自定义变量中的项目

时间:2017-01-19 09:52:25

标签: typescript

我定义了这个界面:

interface ICommand {
   regexp: RegExp;
   callback: Function;
}

interface ICommandList {
   [Phrae: string] : ICommand; 
}

并将变量定义为:

namespace CORE{

export let commands: ICommandList;

// (?: | ) non counting group, not to be consider in params
// ( | )? existing or non existing it is the sam impact (one or zero) (? after the reg)
commands = {
   'test': {
         regexp: /^What is your (?:first|family) name (man|guy)$/,   
         callback: (...parameters:string[])=>alert('my name is: '+parameters[1]) //itemsIdentification,
   },

   'items identification': {
         regexp: /^(What is|What's|Could you please tell me|Could you please give me)?\s?(the)?\s?meaning of (TF|FFS|SF|SHF|FF|Tube Film|Shrink Film|Stretch Hood|Stretch Hood Film|Flat Film)$/,
         callback: (x:string)=>alert('hi 1'+x) //itemsIdentification,
   },

   'ML SoH': {
         regexp: /^(What is|What's|Could you please tell me|Could you please give me) the (stock|inventory) of ML$/,
         callback: ()=>alert('hi 2') //mlSOH,
   },

    'Report stock on hand': {
         regexp: /^(What is|What's) (our|the) (stock|inventory|SoH) of (TF|FFS|SF|SHF|FF|Tube Film|Shrink Film|Stretch Hood|Stretch Hood Film|Flat Film)$/,
         callback: ()=>alert('hi 3') //SoH,
        },

     'Basic Mathematical Opertions': {
               // ?\s? can be used instead of space, also could use /i instead of $/,
                regexp: /^(What is|What's|Calculate|How much is) ([\w.]+) (\+|and|plus|\-|less|minus|\*|\x|by|multiplied by|\/|over|divided by) ([\w.]+)$/,
                callback: ()=>alert('hi 4') //math,
              },
  };
}

我想为这个变量ADD添加in other files个项目,所以我写了另一个文件:

namespace CORE{
    commands += {
        'test2': {
         regexp: /^What is your (?:first|family) name (man|guy)$/,   
         callback: (...parameters:string[])=>alert('my name is: '+parameters[1]) //itemsIdentification,
   }
    };

}

但它给了我错误,如下所示,我的问题是: 如何为此变量添加更多项目?

enter image description here

1 个答案:

答案 0 :(得分:1)

您可以使用属性分配:

commands["test2"] = {
    regexp: /^What is your (?:first|family) name (man|guy)$/,
    callback: (...parameters: string[]) => alert('my name is: ' + parameters[1]) //itemsIdentification,
};