【问题标题】:Exporting namespaces in ambient declarations while referencing other namespaces在环境声明中导出命名空间,同时引用其他命名空间
【发布时间】:2016-12-15 01:38:00
【问题描述】:

我正在尝试为 Turf.js 创建环境类型声明。它可以从 npm 导入,可以包含所有功能,也可以逐个导入,一次一个模块。

我有一个包含所有函数声明的主命名空间:

declare namespace turf {
    export function area(input: GeoJSON.Feature<any> | GeoJSON.FeatureCollection<any>): number;
    export function bearing(start: GeoJSON.Feature<GeoJSON.Point>, end: GeoJSON.Feature<GeoJSON.Point>): number;
    ...
    export function feature(geometry: GeoJSON.GeometryObject, properties?: any): GeoJSON.FeatureCollection<any>;
    export function point(coordinates: GeoJSON.Position, properties?: any): GeoJSON.Feature<GeoJSON.Point>;

}

对于单片导出,我可以这样做:

declare module "turf" {
    export = turf;
}

对于正常的零散模块导出,我可以这样做:

declare module "@turf/area" {
    export default turf.area;
}

declare module "@turf/bearing" {
    export default turf.bearing;
}

然后可以这样使用:

import bearing from "@turf/bearing";

turf 库还有一个名为@turf/helpers 的模块,使用方式如下:

import { feature, point } from "@turf/helpers"

我的问题是,如何键入“@turf/helpers”模块的导出?我不想复制类型定义,而是使用 turf 命名空间中已经存在的定义。

我能做到的最接近的是:

declare module "@turf/helpers" {
    export = {
        feature: turf.feature,
        point: turf.point,
    }
}

但由于我没有导出模块,所以我必须在我的实现中使用import x = require(...) 格式:

import helpers = require("@turf/helpers");
const { feature, point } = helpers;
feature(...)

【问题讨论】:

    标签: typescript typescript-typings


    【解决方案1】:

    使用export import ... = ... 构造解决了这个问题。

    (旁注:我还了解到并非@turf/helpers 包中的所有功能都可以在@turf 中使用,这将在distanceToDegrees 中进行说明。)

    我在 turf 命名空间中创建了一个嵌套命名空间,然后明确声明应该从 helpers 命名空间中导出哪些声明:

    declare namespace turf {
        ...
        namespace helpers {
            // Should be imported as turf.feature and turf.point
            export function feature(geometry: GeoJSON.GeometryObject, properties?: any): GeoJSON.FeatureCollection<any>;
            export function point(coordinates: GeoJSON.Position, properties?: any): GeoJSON.Feature<GeoJSON.Point>;
            ...
            // Should not be included in turf, only able to be imported from @turf/helpers
            export function distanceToDegrees(distance: number, unit?: Unit): number;
        }
    
        export import feature = helpers.feature;
        export import point = helpers.point;
    }
    

    “草皮”整体模块声明保持不变:

    declare module "turf" {
        export = turf;
    }
    

    “@turf/helpers”模块声明如下所示:

    declare module "@turf/helpers" {
        import helpers = turf.helpers;
        export = helpers;
    }
    

    所以现在两种用例都支持:

    import * as turf from "turf";
    
    import { feature, distanceToDegrees } from "@turf/helpers";
    
    let geometry = {
      "type": "Point",
      "coordinates": [
        67.5,
        32.84267363195431,
      ],
    };
    
    turf.feature(geometry); // OK 
    feature(geometry); // OK
    turf.distanceToDegrees(10) // NOT OK
    distanceToDegrees(10); // OK
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 1970-01-01
      • 2011-01-31
      • 1970-01-01
      • 2020-01-14
      • 2018-07-24
      相关资源
      最近更新 更多