2013-04-18 34 views
6

我正在使用LeafletJS绘制出平面图。我最近在DevIntersection上学到了一点关于TypeScript的知识,并想开始转换我的所有JS以使用它。幸运的是,有人已经为Leaflet创建了定义文件,但是我使用的一个插件没有一个(MarkerCluster)。MarkerCluster LeafletJS插件TypeScript定义文件创建

我有插件到它编译的地步(在传单定义文件的一些小的添加之后),但是当我尝试使用它时,我没有看到它的任何方法(参见下面的使用示例)。我也尝试从它创建一个定义文件,但它创建的是空的(使用tsc --declaration)。自定义文件传单和插件都有点长我上传他们:

leaflet.d.tsleaflet.markercluster.ts

用法:

/// <reference path="typings/jquery/jquery.d.ts" /> 
/// <reference path="typings/jqueryui/jqueryui.d.ts" /> 
/// <reference path="typings/leaflet/leaflet.d.ts" /> 
/// <reference path="typings/leaflet.markercluster.ts" /> 

module FloorPlans 
{ 
    export class Floor 
    { 
     deskMarkers : L.MarkerClusterGroup; // <-- Compile error 
     peopleMarkers: L.MarkerClusterGroup; // <-- Compile error 
     tileLayer: L.TileLayer; 
     desks = new Object(); 
     people = new Object(); 

     constructor(public floorName: string, public floorID: number) { } 

    } 
} 

错误:

The property 'MarkerClusterGroup' does not exist on value of type 'L'

任何意见或指导从哪里出发?

+0

小麻烦,但它是(对于我们这些想帮助你更容易),如果你创建了一个要点,以托管文件更好(HTTP ://gist.github.com)。 –

+0

@AlexDresko不知道这一点 - 我将确保下次使用它。谢谢! – Doug

回答

5

有很多不同的约定的JavaScript中创建“类”和打字稿没有按”不了解任何关于他们的事情。您在leaflet.markercluster.ts中拥有的是合法的JavaScript,因此是合法的TypeScript,但TypeScript不会将其分解为类,因为TypeScript可以理解它们,这就是为此生成的声明文件为空的原因。

除极少数情况下声明文件是手动创建的,这可能是您在这里必须执行的操作。它可能会开始像这样:

/// <reference path="leaflet.d.ts" /> 
declare module L { 
    export class MarkerClusterGroup extends FeatureGroup { 
     initialize(options: any): void; 
     addLayer(layer:ILayer):MarkerClusterGroup; 
     addLayer(layer:LayerGroup):MarkerClusterGroup; 
     // and so on and so forth 
    } 
} 

我不得不创建几个文件声明自己和学习曲线短后它不是太难。我发现this blog post对于入门(道具如果你正在阅读这篇文章的道具),然后通过绝对的例子学习很有帮助。

一旦你对你的宣言文件感到满意,请记住回馈给绝对温暖的模糊感受。

+0

感谢您的详细信息,并且博客条目看起来非常有帮助。一旦我获得了这个良好的状态,我将会把它返回给明确的 – Doug

+1

+1'温暖的模糊感受' – MiMo

2

leaflet.marketcluster.ts似乎只是JavaScript代码,它不包含任何TypeScript类声明。

您应该创建一个定义文件leaflet.marketcluster.d.ts代替(并保持原来的leaflet.marketcluster代码只是JavaScript的):

declare module L { 

    export interface MarkerClusterGroupOptions { 
     maxClusterRadius?: number; 
     // etc. 
    }, 

    export class MarketClusterGroup extends FeatureGroup { 
     initialize(options: MarketClusterGroupOptions); 
     addLayer(layer: LayerGroup); 
     // etc. 
    } 

    // . . . etc. . . . 
} 
+0

太棒了,谢谢!这有助于清理我需要做的事 – Doug

4

它看起来对我来说,贡献从来没有发生过,所以我就提交了一份公关DefinitelyTyped为leaflet.markercluster让那些温暖和模糊的感觉:)

拥有最常见的选择,但显然会爱如果有人增加了更多的(并写更多的测试!)

https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/leaflet.markercluster/index.d.ts

+0

任何机会,你可以评论类似的声明,我试图为传单路径转换?提前致谢! http://stackoverflow.com/questions/42303181 – carpiediem