如何在 TypeScript 中创建环境类声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12744147/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:12:18  来源:igfitidea点击:

How do I create Ambient Class Declarations in TypeScript

typescript

提问by Ralph de Ruijter

I am trying to create a .d.ts file for the KineticJS library. So far I have created the following interface declaration "kinect.d.ts".(I cropped the code a bit for stackoverflow but I hope you get the idea)

我正在尝试为 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
        */
    }
}

I hoped this would be enough to be able to create a Kinetic.Rect object in my app.ts file

我希望这足以在我的 app.ts 文件中创建 Kinetic.Rect 对象

/// <reference path="Kinetic.d.ts" />
var rect = new Kinetic.Rect({
          x: 239,
          y: 75,
          width: 100,
          height: 50        
        });

But it appears I have to do some extra work to use the KineticJS classes (like Rect) in TypeScript. Could anyone give some pointers on how to archive this?

但似乎我必须做一些额外的工作才能在 TypeScript 中使用 KineticJS 类(如 Rect)。任何人都可以就如何存档此问题提供一些指示吗?

采纳答案by camelCase

Have you looked at the TypeScript example app at: http://typescript.codeplex.com/SourceControl/changeset/view/fe3bc0bfce1f#samples/imageboard/mongodb.ts

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

The code at this link creates a definition for the mongodb library. One difference between this and the Sohnee answer is that Sohnee implements the constructor in contrast to the following code snip from the link which is a stub class. I do not have enough reputation to ask Sohnee in the accepted answer why he implemented the constructor for an ambient class?

此链接中的代码为 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);

回答by Fenton

Here is my working example of creating ambient definitions for your Kinetic class:

这是我为 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        
});

Note that I have used declare var Kinetic: IKinetic;to tell TypeScript that Kinetic is of the particular type.

请注意,我曾经declare var Kinetic: IKinetic;告诉 TypeScript Kinetic 是特定类型的。

Update - Example 2

更新 - 示例 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        
});

回答by Pavel Savara

ITodoStorage is really interface, TodoStorage is implementation, but I would not like to define class, because that would force me to implement all members. Instead I make TodoStorage interface as well. Finally I declare var as constructor with new keyword.

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;
}

Then I'm able to call the constructor

然后我可以调用构造函数

var storageService : ITodoStorage = new TodoStorage();

Unfortunately the var is hiding the TodoStorage type.

不幸的是,var 隐藏了 TodoStorage 类型。

回答by Chris Rogers

I realise this is now old, but you can find a completed kinetic.d.ts file here : http://kineticjstypescript.codeplex.com/

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