我正在尝试为 KineticJS 库创建一个 .d.ts 文件。到目前为止,我已经创建了以下接口(interface)声明“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)。谁能给出一些关于如何存档的指示?
最佳答案
您是否查看过 TypeScript 示例应用程序:http://typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#samples/imageboard/mongodb.ts
此链接中的代码为 mongodb 库创建了一个定义。这与 Sohnee 答案之间的一个区别是 Sohnee 实现了构造函数,这与来自 stub 类链接的以下代码片段形成对比。我没有足够的声誉在接受的答案中问 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);
关于typescript - 如何在 TypeScript 中创建环境类声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12744147/