我正在尝试为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
*/
}
}
Run Code Online (Sandbox Code Playgroud)
我希望这足以能够在我的app.ts文件中创建一个Kinetic.Rect对象
/// <reference path="Kinetic.d.ts" />
var rect = new Kinetic.Rect({
x: 239,
y: 75,
width: 100,
height: 50
});
Run Code Online (Sandbox Code Playgroud)
但似乎我必须做一些额外的工作来在TypeScript中使用KineticJS类(如Rect).任何人都可以提供一些关于如何存档的指示吗?
您是否查看过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);
Run Code Online (Sandbox Code Playgroud)
以下是为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
});
Run Code Online (Sandbox Code Playgroud)
请注意,我曾经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
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11307 次 |
| 最近记录: |