微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

库插件的打字稿声明文件

如何解决库插件的打字稿声明文件

我为leaflet-arrowheads写了一个插件leaflet,它扩展了传单的polyline类。我有一个创建打字稿声明文件的请求。我正在尝试根据instructions from the typescript docs解决问题,但是我不确定自己是否做对了。我的项目目录如下:

project
-node_modules
-src
 | -index.js
 | -leaflet-arrowheads.js
package.json

我的理解是,我想在我的模块所在的index.d.ts文件夹中粘贴一个src磁贴。因此,我将这样做:

project
-node_modules
-src
 | -index.js
 | -index.d.ts
 | -leaflet-arrowheads.js
package.json

问题1:这是正确的方法吗?还是应该将此文件添加到“ leaflet-arrowheads / index.d.ts”下的DefinitelyTyped / types文件夹中,并指示人们使用npm i @types/leaflet-arrowheads

@types.leaflet开始,传单有自己的打字稿标记。 (您可以看到代码here),它定义了折线上可用的方法类型。我只是想补充一下,因为我的插件只是向L.polyline添加了一些方法polyline类型定义如下:

export class polyline<T extends geojson.GeometryObject = geojson.Linestring | geojson.MultiLinestring,P = any> extends Path {
    constructor(latlngs: LatLngExpression[] | LatLngExpression[][],options?: polylineoptions);
    toGeoJSON(): geojson.Feature<T,P>;
    getLatLngs(): LatLng[] | LatLng[][] | LatLng[][][];

    // ... a few more method and options deFinitions
}

问题2:如何向此定义添加一些方法?到目前为止,我的index.d.ts文件中有此文件

export interface ArrowheadOptions {
   yawn?: number;
   size?: string | number;
   frequency?: string | number;
   proportionalToTotal?: false;
}

export interface polyline {
   arrowheads: (options?: ArrowheadOptions) => any;
   buildVectorHats: () => any;
   getArrowheads: () => any;
   deleteArrowheads: () => any;
}

我的理解是,这只会将这些选项合并到现有的polyline类型定义中。那是对的吗?我要走吗?我看到了其他使用如下语法的插件

import * as L from 'leaflet'

declare module leaflet {

  // export interfaces here

}

其他人再次使用这种语法:

import { Control } from 'leaflet'

declare module 'leaflet {
  namespace Control {
    interface ControlOptions{
      // some deFinitions here
    }
  }
} 

如果这是一个简单的问题,请原谅我,对于这样的特定情况,我发现打字稿文档有些混乱。我想确保自己编写正确,以便我的定义适合Leaflet及其L.polyline的现有命名空间。问题3:我在index.d.ts文件中执行操作的方式与此处的其他2个示例之间有什么区别?感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。