【发布时间】: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