来自Javascript的TypeScript定义文件

时间:2015-10-26 22:02:02

标签: typescript

我有以下 Js 文件,这是一个传单插件,

JS:

   L.BingLayer = L.TileLayer.extend({
options: {
    subdomains: [0, 1, 2, 3],
    type: 'Aerial',
    attribution: 'Bing',
    culture: ''
},

initialize: function(key, options) {
    L.Util.setOptions(this, options);

    this._key = key;
    this._url = null;
    this.meta = {};
    this.loadMetadata();
},

tile2quad: function(x, y, z) {
    var quad = '';
    for (var i = z; i > 0; i--) {
        var digit = 0;
        var mask = 1 << (i - 1);
        if ((x & mask) !== 0) digit += 1;
        if ((y & mask) !== 0) digit += 2;
        quad = quad + digit;
    }
    return quad;
},

getTileUrl: function(p, z) {
    var zoom = this._getZoomForUrl();
    var subdomains = this.options.subdomains,
        s = this.options.subdomains[Math.abs((p.x + p.y) % subdomains.length)];
    return "something";
},

loadMetadata: function() {
    var _this = this;
    var cbid = '_bing_metadata_' + L.Util.stamp(this);
    window[cbid] = function (meta) {
        _this.meta = meta;
        window[cbid] = undefined;
        var e = document.getElementById(cbid);
        e.parentNode.removeChild(e);
        if (meta.errorDetails) {
            return;
        }
        _this.initMetadata();
    };
//  var urlScheme = (document.location.protocol === 'file:') ? 'http' : document.location.protocol.slice(0, -1);    
},

initMetadata: function() {
    var r = this.meta.resourceSets[0].resources[0];
    this.options.subdomains = r.imageUrlSubdomains;
    this._url = r.imageUrl;
    this._providers = [];
    if (r.imageryProviders) {
        for (var i = 0; i < r.imageryProviders.length; i++) {
            var p = r.imageryProviders[i];
            for (var j = 0; j < p.coverageAreas.length; j++) {
                var c = p.coverageAreas[j];
                var coverage = {zoomMin: c.zoomMin, zoomMax: c.zoomMax, active: false};
                var bounds = new L.LatLngBounds(
                        new L.LatLng(c.bbox[0]+0.01, c.bbox[1]+0.01),
                        new L.LatLng(c.bbox[2]-0.01, c.bbox[3]-0.01)
                );
                coverage.bounds = bounds;
                coverage.attrib = p.attribution;
                this._providers.push(coverage);
            }
        }
    }
    this._update();
},

_update: function() {
    if (this._url === null || !this._map) return;
    this._update_attribution();
    L.TileLayer.prototype._update.apply(this, []);
},

_update_attribution: function() {
    var bounds = this._map.getBounds();
    var zoom = this._map.getZoom();
    for (var i = 0; i < this._providers.length; i++) {
        var p = this._providers[i];
        if ((zoom <= p.zoomMax && zoom >= p.zoomMin) &&
                bounds.intersects(p.bounds)) {
            if (!p.active && this._map.attributionControl)
                this._map.attributionControl.addAttribution(p.attrib);
            p.active = true;
        } else {
            if (p.active && this._map.attributionControl)
                this._map.attributionControl.removeAttribution(p.attrib);
            p.active = false;
        }
    }
},

onRemove: function(map) {
    for (var i = 0; i < this._providers.length; i++) {
        var p = this._providers[i];
        if (p.active && this._map.attributionControl) {
            this._map.attributionControl.removeAttribution(p.attrib);
            p.active = false;
        }
    }
        L.TileLayer.prototype.onRemove.apply(this, [map]);
    }
  });

L.bingLayer = function (key, options) {
return new L.BingLayer(key, options);
};

我为这个js文件编写了定义文件,这里是

d.ts

declare namespace L {

export interface BingLayer extends L.TileLayer {
    options: any;
    initialize(key: any, options: any): void;
    tile2quad(x: any, y: any, z: any): any;
    getTileUrl(p: any, z: any): any;
    loadMetadata(): void;
    initMetadata(): void;
    _update(): void;
    _update_attribution: void;
    onRemove(map: any): void;       
}

export interface BingLayerFactory {
    (key: any, options: any): BingLayer;
    }
}

两个文件都在同一个文件夹中,当我尝试使用该界面时,Visual Studio在输入 L 之后确实向我显示了 BingLayer ,但抱怨无法 BingLayer 在我搬到下一行之后。谁能告诉我我的错误在哪里?

1 个答案:

答案 0 :(得分:0)

  

当我尝试使用该界面时,Visual Studio确实在我输入L之后向我显示了BingLayer但是在我移到下一行后抱怨不能BingLayer

以下应该可以正常工作:

var foo : L.BingLayer;

我怀疑你在做:

var foo : BingLayer; // Error

或者您的import文件中有根级exportd.ts。这会将文件与全局命名空间(more

断开连接