Typescript 组合类和接口类型
在Javascript中(在WScript或HTA下),我将编写以下模式:Typescript 组合类和接口类型,typescript,activexobject,Typescript,Activexobject,在Javascript中(在WScript或HTA下),我将编写以下模式: var fso = new ActiveXObject('Scripting.FileSystemObject'); var ext = fso.GetExtensionName('C:\test.txt'); //returns 'txt' var badResult = fso.NonExistentMethod(); //raises a runtime error 使用TypeScrip
var fso = new ActiveXObject('Scripting.FileSystemObject');
var ext = fso.GetExtensionName('C:\test.txt'); //returns 'txt'
var badResult = fso.NonExistentMethod(); //raises a runtime error
使用TypeScript,我可以声明一个接口来防止此类错误:
module Scripting {
export interface FileSystemObject {
GetExtensionName(path: string): string,
GetParentFolderName(path: string): string
}
}
var fso: Scripting.FileSystemObject = new ActiveXObject('Scripting.FileSystemObject');
var ext = fso.GetExtensionName('C:\test.txt'); //ext is recognized as a string
//var badResult = fso.NonExistentMethod(); //will not compile
但是,我想初始化fso
变量,如下所示:
var fso = new Scripting.FileSystemObject();
并让类型系统自动推断fso
的类型是接口Scripting.FileSystemObject
这能做到吗
1) 我无法将构造函数添加到接口,因为接口只是对象的形状,而不是其实现
2) 我考虑创建一个内部接口,并使用类和构造函数扩展接口:
module Scripting {
interface Internal {
GetExtensionName(path: string): string;
GetParentFolderName(path: string): string;
}
export class FileSystemObject extends Internal {
constructor() {
return new ActiveXObject('Scripting.FileSystemObject');
}
}
}
但是,如果不调用基类型构造函数(接口没有基类型构造函数),就不能将构造函数添加到扩展类中
3) 我不能声明一个
类,因为构造函数仍然是一个实现,不能写入声明的类,这是一个环境上下文
推断fso的类型是接口Scripting.FileSystemObject。可以这样做吗
当然。只需声明有一些东西,当使用new
调用时,这些东西将返回正确的类型。i、 e.export var FileSystemObject:{new():FileSystemObject}代码>如下:
declare module Scripting {
export interface FileSystemObject {
GetExtensionName(path: string): string;
GetParentFolderName(path: string): string;
}
export var FileSystemObject: { new (): FileSystemObject };
}
var fso: Scripting.FileSystemObject = new Scripting.FileSystemObject();
var ext = fso.GetExtensionName('C:\test.txt'); //ext is recognized as a string
var badResult = fso.NonExistentMethod(); //will not compile
更新要自己实现类似的功能,您可以执行以下操作:
module Scripting {
export interface FileSystemObject {
GetExtensionName(path: string): string;
GetParentFolderName(path: string): string;
}
export var FileSystemObject: { new (): FileSystemObject } = <any>function(){
return new ActiveXObject('Scripting.FileSystemObject');
};
}
var fso: Scripting.FileSystemObject = new Scripting.FileSystemObject();
var ext = fso.GetExtensionName('C:\test.txt'); //ext is recognized as a string
var badResult = fso.NonExistentMethod(); //will not compile
模块脚本{
导出接口文件系统对象{
GetExtensionName(路径:string):string;
GetParentFolderName(路径:string):string;
}
导出var FileSystemObject:{new():FileSystemObject}=function(){
返回新的ActiveXObject('Scripting.FileSystemObject');
};
}
var fso:Scripting.FileSystemObject=new Scripting.FileSystemObject();
var ext=fso.GetExtensionName('C:\test.txt')//ext被识别为字符串
var badResult=fso.NonExistentMethod()//不会编译
实际上,我不需要fso上的类型注释。然而,我仍然感到困惑。我应该将运行new-Scripting.FileSystemObject()
的构造函数实现放在哪里代码>?我以为你是新的脚本.FileSystemObject()代码>是环境的,即在其他地方用javascript声明。如果要在TypeScript中创建它,请参阅我的更新