Search code examples
typescriptproj

Trying to create a typescript definition for proj4, how to declare anonymous static?


The transormer method below should actually be anonymous but that is not allowed in typescript:

class Proj {
    static (a, b): {
        forward: (p: Point) => Point;
        inverse: (p: Point) => Point;
    };
    static defs(name: string): any;
    static defs(name: string, def: string): void;
    static transform(from: any, to: any, pt: Point);
    static parse(sr: string): any;
}

So how can this be defined such that the following is possible?

import proj = require("proj");
proj("EPSG:3857", "EPSG:4326").forward([0,0]);

Solution

  • Are you looking for something like below? When you declare a function and a Module with the same name (the function needs the be declared before the module or you will get an error) they merge.

    Below is the code you had with some small changes (and I removed some functions).

    interface Item {
        forward: (p: Point) => Point;
        inverse: (p: Point) => Point;
    }
    function Proj(a, b): Item {
        return null;
    }
    module Proj {
        export function defs(name: string): any {
            return null
        }
    }
    
    Proj.defs("");
    Proj(1 ,3).forward(null);