Skip to content

[Leaflet] addHandler and constructor function #20470

@wqoq

Description

@wqoq
  • I tried using the @types/leaflet package and had problems.
  • I tried using the latest stable version of tsc. https://www.npmjs.com/package/typescript
  • I have a question that is inappropriate for StackOverflow. (Please ask any appropriate questions there).
  • Mention the authors (see Definitions by: in index.d.ts) so they can respond.

I'm trying to add a handler to my Leaflet map (actually, I'm trying to manually add a Leaflet draw handler to a map without using it's toolbar), and I'm running into a syntax issue with the addHandler method in L.Map. I'm using TypeScript 2.3.

This is the line in question:

addHandler(name: string, HandlerClass: () => Handler): this; // HandlerClass is actually a constructor function, is this the right way?

Following the example here, I have the following line of code:

this.map.addHandler('polygon', L.Draw.Polygon);

But that gives me the following error:

Argument of type 'typeof Polygon' is not assignable to parameter of type '() => Handler'.

If I change the addHandler line in index.d.ts to:

addHandler(name: string, HandlerClass: typeof Handler): this;

Then the error goes away, and everything works perfectly. Plus, it matches the examples in Leaflet's own documentation.

It seems like the following could be possible, too, no? That's got to be at least a little bit better than any.

export class Class {
    static extend(props: any): typeof Class;
    static include(props: any): typeof Class;
    static mergeOptions(props: any): typeof Class;
    static addInitHook(initHookFn: () => void): typeof Class;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions