typescript为npm包创建一个简单的d.ts文件

时间:2017-02-04 04:26:19

标签: javascript node.js typescript

我正在尝试声明一个.d.ts文件,以便在agular 2应用中使用npm uuid。我正在做的似乎与我找到的例子一致,但我收到了错误:typescript d.ts file has no exported members v1

uuid / v1.d.ts文件:

declare var v1: any;

declare module "uuid/v1" {
    export = v1;
}

npm js文件是 uuid / v1.js

function v1(options, buf, offset) { //...}

module.exports = v1;

我也尝试过:

export = uuid;
declare namespace uuid {
  function v1(): any;
}

给出了运行时异常:

ORIGINAL EXCEPTION: __webpack_require__.i(...) is not a function

1 个答案:

答案 0 :(得分:1)

我假设这与我写一个DefinitelyTyped文件的方式类似。请参阅@types/selenium-webdriver

declare namespace uuid {
  interface V1Options {
    /**
     * Node id as Array of 6 bytes (per 4.1.6).
     * Default: Randomly generated ID.
     */
    node?: Array<any>;
    /**
     * Number between 0 - 0x3fff. RFC clock sequence.
     * Default: An internally maintained clockseq is used
     */
    clockseq?: number;
    /**
     * Time in milliseconds since unix Epoch.
     * Default: The current time is used.
     */
    msecs: number|Date;
    /**
     * Number between 0-9999) additional time, in 100-nanosecond units.
     * Ignored if msecs is unspecified. Default: internal uuid
     * counter is used, as per 4.2.1.2.
     */
    nsecs: number;
  };

  /**
   * Generate and return a RFC4122 v1 (timestamp-based) UUID.
   * @param {V1Options} options Optional uuid state to apply.
   * @param {Array<any>|Buffer} buffer Array or buffer where
   *        UUID bytes are to be written.
   * @param {number} offset Starting index in buffer at which to begin writing.
   */
  v1(options?: V1Options, buffer?: Array<any>|Buffer, offset?: number): Buffer;
}
export = uuid;

我相信您必须指示您的tsconfig.json使用此文件(或将其发布到DefinitelyTyped),然后您可以import {v1, V1Options} from 'uuid';import * from 'uuid';