0

I'm trying to declare a .d.ts file to use npm uuid in an agular 2 app. What I'm doing seems to be in line with the example I found, yet I get the error : typescript d.ts file has no exported members v1

uuid/v1.d.ts file:

declare var v1: any;

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

the npm js file is uuid/v1.js:

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

module.exports = v1;

I also tried :

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

which gives a runtime exception:

ORIGINAL EXCEPTION: __webpack_require__.i(...) is not a function
3
  • How are your importing it? Commented Feb 4, 2017 at 5:55
  • @AluanHaddad like this import { v1 } from 'uuid/v1'; Commented Feb 4, 2017 at 10:11
  • You need to export the function from the namespace. Commented Feb 5, 2017 at 8:22

1 Answer 1

1

I am assuming this will be similar to how I wrote a DefinitelyTyped file. See @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;

I believe you'll have to direct your tsconfig.json to use this file (or get it published to DefinitelyTyped) and then you can import {v1, V1Options} from 'uuid'; or import * from 'uuid';

Sign up to request clarification or add additional context in comments.

2 Comments

There is a couple errors there. after fixing those there is still an error at runtime.
Do you have this in a github repo? I'll take a look at it this weekend.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.