Interface 我可以使用属性列表的接口吗?
我有一个接口def 为了在许多地方使用,我知道我必须将其拆分为自己的文件(至少使用ES5输出)。因此: //ICommand.ts>Interface 我可以使用属性列表的接口吗?,interface,typescript,Interface,Typescript,我有一个接口def 为了在许多地方使用,我知道我必须将其拆分为自己的文件(至少使用ES5输出)。因此: //ICommand.ts> interface RegExp { $1: string; $2: string; $3: string; // etc } interface IBotCommand { regex: RegExp // fixme - clearer def // cmd: ():Output cmd: an
interface RegExp {
$1: string;
$2: string;
$3: string;
// etc
}
interface IBotCommand {
regex: RegExp
// fixme - clearer def
// cmd: ():Output
cmd: any
}
export = IBotCommand;
//BotCommand.ts>
import ICommand = require("./ICommand");
class BotCommand {
commandList: ICommand
但这会产生错误:找不到名称ICommand
这可能吗?最好的语法是什么?似乎这可能是因为我的环境没有重新编译这些脚本 此外,RegExp定义取自此处:
并且可能功能不全您正在像这样导出界面:
export = IBotCommand;
// but you are importing it with:
import ICommand = require('./ICommand');
import ICommand from './ICommand';
export default IBotCommand;
在导出保持原样的情况下,将导入更改为:
import { IBotCommand } from './ICommand';
如果要按如下方式导入:
export = IBotCommand;
// but you are importing it with:
import ICommand = require('./ICommand');
import ICommand from './ICommand';
export default IBotCommand;
然后,您将希望按如下方式导出它:
export = IBotCommand;
// but you are importing it with:
import ICommand = require('./ICommand');
import ICommand from './ICommand';
export default IBotCommand;
如果不使用默认值
,则必须使用对象表示法来引用导入
对象表示法有一个警告,您也可以这样做:
import * as cmds from './ICommand';
// and use it like this:
class SomeClass implements cmds.IBotCommand {}
这是ES6语法吗?我现在的目标是ES5。哪一个更可靠?这是利用es6模块语法,但TypeScript编译器可以配置为输出ES5代码