How to use custom d.ts in Visual Studio 2015

621 Views Asked by At

I am new to TypeScripts, for the most part I use the DefinitelyTyped NuGet packages to get type information for the libraries I use. Every now and then I run into a library that there are no type definitions for, in this case proj4. I managed to find a couple of proj4.d.ts-files on internet that seemed fine, but when I add this file to a folder Scripts/typings/proj4.d.ts, and then attempt to use proj4(...) in my .ts file, proj4 cant be found.

If I examine my other d.ts-files (eg knockout.d.ts), I cant really see any difference between that file and my proj4.d.ts, my file has Build Action TypeScriptCompile.

Is there something else that needs to be done to "register" the proj4.d.ts file (other than simply copying it to a folder sitting next to the existing type definition files)? I am using Visual Studio 2015 and the TypeScript 1.6.3 plugin. Here is the file that I am attempting to use proj4.d.ts from ca0v on GitHub:

declare module "proj4" {

interface Transformer {
    forward: (p: Point) => Point;
    inverse: (p: Point) => Point;
}

class Point {
    x: number;
    y: number;
    constructor(x: number, y: number);
}

function Proj(a, b): Transformer;

module Proj {
    export function defs(name: string): any;
    export function defs(name: string, def: string): void;
    export function transform(from: any, to: any, pt: Point): Point;
    export function parse(sr: string): any;
}

export = Proj;

}

1

There are 1 best solutions below

0
Emil G On

Finally, I solved this issue by creating a new proj4.d.ts file. This file is inspired by the jquery.d.ts and it works right out of the box (like the other type definition files that I normally use from DefinitelyTyped. There might not have been anything wrong at all with the original file that I mentioned above, just that for my setup (VS 2015/TypeScript plugin 1.6.3/No external compilation/limited knowledge on modules/imports in TypeScript) this file works better.

In order to use this file, simply copy it into /Your...scripts/typings/proj4/proj4.d.ts. This file is also on GitHub/ Stockholmsnovis/MyTypeScriptDefinitions/proj4.d.ts

disclamer: My type definition file is far from complete, just the functions from proj4 that I need right now, feel free to extend

proj4.d.ts:

// Type definitions for proj4 2.3.x
// Project: http://proj4js.org/
// Definitions by: Emil Goude <https://github.com/Stockholmsnovis>

interface Proj4Point {
  x: number,
  y: number;
}

interface Proj4Static {

    /**
     * Transforms specified coordinates from one coordinate system to another.
     *
     * @param fromProjection A string containing the projection definition to  transform from (or a named projection is such projection exists in proj4.defs(...)), see http://spatialreference.org/ref/ for examples. Projection can be a proj or a wkt string.
     * @param toProjection A string containing the projection definition  to transform to (or a named projection is such projection exists in proj4.defs(...)), see http://spatialreference.org/ref/ for examples. Projection can be a proj or a wkt string.
     * @param point the coordinates to transform (using the coordinate system of the fromProjection).
    */
    (fromProjection: string, toProjection: string, point: Proj4Point): Proj4Point;

    /**
     * Transforms specified coordinates from WGS84 coordinates system to another coordinate system.
     *
     * @param toProjection A string containing the projection definition  to transform to (or a named projection is such projection exists in proj4.defs(...)), see http://spatialreference.org/ref/ for examples of projection strings.  Projection can be a proj or a wkt string.
     * @param point the coordinates to transform (using the coordinate system of the fromProjection).
    */
    (toProjection: string, point: Proj4Point): Proj4Point;

    /**
     * Defines a named projection in proj4. Once a projection is defined using this method, you may use the proj4(...) method(s) with the projection name instead of the full projection string, , see http://spatialreference.org/ref/ for examples on projection strings.
     * @param projectionName 
     * @param projectionString 
     */
    defs(projectionName: string, projectionString:string):void;

    /**
     * Returns the projection (if any) in proj4.
     * @param projectionName 
     */
    defs(projectionName: string): string;

}

declare module "proj4" {
    export = proj4;
}

declare var proj4: Proj4Static;

And here is how you would use it, eg: myscript.ts:

// Note, no need to reference proj4.d.ts (never needed anymore(?))
// Define coodrinate systems, see http://spatialreference.org/ref/ for more info:
proj4.defs("ESPG:3006", "+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs");
var result = proj4(namedProjection, <Proj4Point>{ x: 18.110103, y: 59.334415 });