我正在尝试为KineticJS库创建一个.d.ts文件。到目前为止,我已经创建了以下接口声明“kinect.d.ts”。(我为stackoverflow删了一些代码,但我希望你能得到这个想法)
module Kinetic {
interface Rect extends Shape {
constructor (config) ;
}
interface Shape extends Node
{
}
interface Node {
constructor (config);
clone(attrs): Node;
getAbsoluteOpacity(): number;
getAbsolutePosition(): any;
/*
other methods removed for stackoverflow example
*/
}
}
我希望这足以在我的app.ts文件中创建一个Kinetic.Rect对象
/// <reference path="Kinetic.d.ts" />
var rect = new Kinetic.Rect({
x: 239,
y: 75,
width: 100,
height: 50
});
但似乎我需要做一些额外的工作来在TypeScript中使用KineticJS类(比如Rect)。任何人都可以提供一些关于如何存档的指示吗?
答案 0 :(得分:6)
此链接的代码为mongodb库创建定义。这和Sohnee答案之间的一个区别是Sohnee实现了构造函数,而下面的代码片段是来自链接的代码段,而这是一个存根类。我没有足够的声誉在接受的答案中询问Sohnee为什么他为环境类实现了构造函数?
declare module "mongodb" {
export class Server {
constructor(host: string, port: number, opts?: any, moreopts?: any);
}
export class Db {
constructor(databaseName: string, serverConfig: Server);
public open(callback: ()=>void);
答案 1 :(得分:5)
以下是我为Kinetic类创建环境定义的工作示例:
interface Shape {
x: number;
y: number;
width: number;
height: number;
}
interface IKinetic {
Rect(shape: Shape);
}
declare var Kinetic: IKinetic;
var rect = <Shape> new Kinetic.Rect({
x: 239,
y: 75,
width: 100,
height: 50
});
请注意,我使用declare var Kinetic: IKinetic;
告诉TypeScript Kinetic是特定类型的。
更新 - 示例2
interface IShape {
x: number;
y: number;
width: number;
height: number;
}
interface IRect extends IShape {
}
module Kinetic {
export class Rect implements IRect {
public x: number;
public y: number;
public width: number;
public height: number;
constructor(rect: IShape) {
this.x = rect.x;
this.y = rect.y;
this.width = rect.width;
this.height = rect.height;
}
}
}
var rect = new Kinetic.Rect({
x: 239,
y: 75,
width: 100,
height: 50
});
答案 2 :(得分:0)
ITodoStorage实际上是接口,TodoStorage是实现,但我不想定义类,因为这会迫使我实现所有成员。相反,我也制作了TodoStorage界面。最后,我将var声明为带有new关键字的构造函数。
declare interface ITodoStorage {
get_todos() : TodoItem[];
set_todos(value : TodoItem[]) : void;
}
declare interface TodoStorage extends ITodoStorage {
}
declare var TodoStorage : {
new (): TodoStorage;
}
然后我就可以调用构造函数
了var storageService : ITodoStorage = new TodoStorage();
不幸的是,var隐藏了TodoStorage类型。
答案 3 :(得分:0)
我意识到这已经过时了,但你可以在这里找到一个完整的kinetic.d.ts文件:http://kineticjstypescript.codeplex.com/