如何访问模块中的TypeScript定义?

时间:2015-01-27 14:35:07

标签: node.js types module typescript

我试图输入一些“网址”' NodeJS definition中描述的对象。但是' Url'对象在' url'下描述。模块。因为它在模块下不被编译器识别为有效类型。

///<reference path='../../../Definitions/node.d.ts' />
///<reference path='../../../Definitions/node-webkit.d.ts' />
///<reference path='../../../Definitions/cheerio.d.ts' />

var NodeUrl = require('url');

function findLinks($cheerio:Cheerio, internalOnly?:boolean, rootUrl?:url.Url):string[] {
    //url.Url is not recognised, neither is Url or NodeUrl.Url
}

定义文件定义了以下模块(与NodeJS的其余部分一起):

declare module "url" {
    export interface Url {
        href: string;
        protocol: string;
        auth: string;
        hostname: string;
        port: string;
        host: string;
        pathname: string;
        search: string;
        query: any; // string | Object
        slashes: boolean;
        hash?: string;
        path?: string;
    }

    export interface UrlOptions {
        protocol?: string;
        auth?: string;
        hostname?: string;
        port?: string;
        host?: string;
        pathname?: string;
        search?: string;
        query?: any;
        hash?: string;
        path?: string;
    }

    export function parse(urlStr: string, parseQueryString?: boolean , slashesDenoteHost?: boolean ): Url;
    export function format(url: UrlOptions): string;
    export function resolve(from: string, to: string): string;
}

如何正确引用网址类型?

感谢您的帮助。

1 个答案:

答案 0 :(得分:5)

在Typescript中,您需要使用稍微不同的语法来导入模块:

import NodeURL = require('url');

您现在可以访问

NodeURL.Url