如何在Visual Studio 2015中使用自定义d.ts

时间:2016-06-02 14:29:44

标签: visual-studio typescript typescript1.6

我是TypeScripts的新手,在大多数情况下,我使用DefinitelyTyped NuGet包来获取我使用的库的类型信息。我偶尔会遇到一个没有类型定义的库,在本例中是 proj4 。我设法在互联网上找到几个proj4.d.ts文件似乎很好,但当我将此文件添加到文件夹Scripts / typings / proj4.d.ts,然后尝试使用proj4(...)在我的.ts文件中,无法找到proj4。

如果我检查我的其他d.ts文件(例如knockout.d.ts),我真的看不到该文件和我的proj4.d.ts之间的任何区别,我的文件有Build Action TypeScriptCompile。

是否需要做其他事情来注册" proj4.d.ts文件(除了简单地将其复制到现有类型定义文件旁边的文件夹)? 我使用的是Visual Studio 2015和TypeScript 1.6.3插件。这是我尝试使用的文件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 个答案:

答案 0 :(得分:0)

最后,我通过创建一个新的proj4.d.ts文件解决了这个问题。这个文件的灵感来自于jquery.d.ts,它开箱即用(就像我通常从DefinitelyTyped使用的其他类型定义文件一样。原始文件可能没有任何错误。我在上面提到,只是为了我的设置(VS 2015 / TypeScript插件1.6.3 /没有外部编译/有关TypeScript中模块/导入的有限知识),这个文件效果更好。

要使用此文件,只需将其复制到/Your...scripts/typings/proj4/proj4.d.ts即可。此文件也在GitHub/ Stockholmsnovis/MyTypeScriptDefinitions/proj4.d.ts

disclamer:我的类型定义文件远非完整,只是我现在需要的proj4中的函数,随意扩展

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;

以下是你将如何使用它,例如: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 });