8

我正在尝试为 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)。任何人都可以就如何存档这个提供一些指示吗?

4

4 回答 4

8

您是否查看过 TypeScript 示例应用程序: http: //typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#samples/imageboard/mongodb.ts

此链接上的代码为 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);
于 2012-10-05T13:50:14.407 回答
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        
});
于 2012-10-05T10:24:57.437 回答
0

ITodoStorage 是真正的接口,TodoStorage 是实现,但我不想定义类,因为那会迫使我实现所有成员。相反,我也制作了 TodoStorage 接口。最后,我使用 new 关键字将 var 声明为构造函数。

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 类型。

于 2012-11-24T23:24:01.907 回答
0

我意识到这已经过时了,但你可以在这里找到一个完整的 kinetic.d.ts 文件:http: //kineticjstypescript.codeplex.com/

于 2013-01-31T04:17:32.333 回答