Javascript 在新的typescript文件(0.9)中使用现有的AMD js模块
我正在尝试使用纯javascript编写的现有AMD和使用TypeScript v0.9的新TypeScript文件。我已经把它分解成一个非常简单的AMD来演示这个问题 首先,我现有的AMD(activityProperty.js): 其次,我使用现有AMD的接口定义创建了activityProperty.d.ts:Javascript 在新的typescript文件(0.9)中使用现有的AMD js模块,javascript,typescript,amd,Javascript,Typescript,Amd,我正在尝试使用纯javascript编写的现有AMD和使用TypeScript v0.9的新TypeScript文件。我已经把它分解成一个非常简单的AMD来演示这个问题 首先,我现有的AMD(activityProperty.js): 其次,我使用现有AMD的接口定义创建了activityProperty.d.ts: export interface IActivityProperty { key: string; value: string; } 最后,我创建了一个全新的Ty
export interface IActivityProperty {
key: string;
value: string;
}
最后,我创建了一个全新的TypeScript类(PropertyBag.ts),我希望在其中使用旧的activityProperty AMD:
import PropertyModule = require("activityProperty");
class PropertyBag {
private properties: Array<PropertyModule.IActivityProperty>;
constructor() {
this.properties = new Array<PropertyModule.IActivityProperty>();
//this is where my problem is...
//this.properties.push(???new activityProperty()???);
}
}
导入属性模块=require(“activityProperty”);
类PropertyBag{
私有属性:数组;
构造函数(){
this.properties=新数组();
//这就是我的问题所在。。。
//this.properties.push(?new activityProperty()?);
}
}
我一辈子都不知道如何使用旧的AMD定义来创建activityProperty
类的新实例。我是否遗漏了一些琐碎或明显的东西
谢谢你的帮助 当前您的声明只有一个接口。将其声明为类以显示它可以被更新和/或扩展
declare class ActivityProperty {
constructor(public key: string, public value: string);
}
您可以决定是否保留您的接口。您的代码不会编译成可以直接与外部AMD模块一起工作的JavaScript,因为AMD模块导出的是类本身,而不是具有指向您的类的IActivityProperty属性的命名空间 您需要以以下方式定义系统: activityProperty.d.ts:
declare module "activityProperty"
{
class activityProperty {
constructor(key: KeyType, value: ValueType);
key: KeyType;
value: ValueType;
}
export = activityProperty;
}
declare module "activityProperty"
{
class activityProperty<K, T> {
constructor(key: K, value: T);
key: K;
value: T;
}
export = activityProperty;
}
PropertyBag.ts:
/// <reference path="activityProperty.d.ts" />
import activityProperty = module("activityProperty");
this.properties = new Array<activityProperty>();
this.properties.push(new activityProperty(key, value));
/// <reference path="activityProperty.d.ts" />
import activityProperty = module("activityProperty");
var x = new activityProperty("hello", 123); // Type inferred to be activityProperty<string, number>
//
导入activityProperty=模块(“activityProperty”);
this.properties=新数组();
this.properties.push(新的activityProperty(键,值));
这将直接编译成与外部AMD模块一起工作的JavaScript。然而,我认为你真正想要的是:
activityProperty.d.ts:
declare module "activityProperty"
{
class activityProperty {
constructor(key: KeyType, value: ValueType);
key: KeyType;
value: ValueType;
}
export = activityProperty;
}
declare module "activityProperty"
{
class activityProperty<K, T> {
constructor(key: K, value: T);
key: K;
value: T;
}
export = activityProperty;
}
声明模块“activityProperty”
{
类活动属性{
构造函数(键:K,值:T);
关键词:K;
值:T;
}
导出=活动属性;
}
PropertyBag.ts:
/// <reference path="activityProperty.d.ts" />
import activityProperty = module("activityProperty");
this.properties = new Array<activityProperty>();
this.properties.push(new activityProperty(key, value));
/// <reference path="activityProperty.d.ts" />
import activityProperty = module("activityProperty");
var x = new activityProperty("hello", 123); // Type inferred to be activityProperty<string, number>
//
导入activityProperty=模块(“activityProperty”);
var x=新的activityProperty(“hello”,123);//推断为activityProperty的类型
谢谢你的回答,史蒂夫。然而,我只是把它作为一个小规模的例子,因为我们有AMD的定义,这是很大的,我正在寻找一种方法,以这种方式使用它们,而无需重写。因此,想象一下界面甚至不存在。有没有一种方法可以使用TypeScript中第一个define
语句中定义的类?好的,这是大部分答案,但我必须添加新的export=activityProperty
获取要由使用该类的新TypeScript类识别的类。感谢您和@stephen chung的帮助!在这个例子中,用TypeScript以难以置信的简单方式重写AMD时,还有一些AMD根本就不是那么简单。我希望在新的TypeScript文件中使用现有的JS AMD定义。在这种情况下,您可能会抵消TypeScript的大部分优点,即静态类型。为了使用静态类型,您总是需要一个.d.ts定义文件。。。否则,一个简单的“declare var activityProperty;”或任何外部根对象将允许您与外部JavaScript进行互操作,但没有类型检查的好处。另外,请注意,您不必在TypeScript中重写AMD。在您的示例中,activityProperty.js保持原样。您只需要一个与js匹配的定义文件(例如activityProperty.d.ts)。通过以我描述的格式进行编码,编译后的JavaScript(即propertyBag.js)在使用AMD加载程序运行时自动加载activityProperty.js。感谢Stephen,在您和Steve Fenton的回答之间,我能够解决这个问题,并将旧的AMD与新的TypeScript类一起使用!