2

In my TypeScript project I want to use a Node.js module called "pure-uuid".

With plain JavaScript, "pure-uuid" can be used as follows:

const UUID = require('pure-uuid')
const id = new UUID(4).format();

I translated the code into TypeScript:

import UUID from 'pure-uuid';
const id:string = new UUID(4).format();

When I compile the code, the following is being generated:

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var pure_uuid_1 = require("pure-uuid");
var id = new pure_uuid_1.default(4).format();

Unfortunately TypeScript adds a .default to the "pure-uuid" reference, which makes the code fail on execution:

TypeError: pure_uuid_1.default is not a constructor

I think the wrong compilation is caused by a mistake in the TypeScript definition file (which has been manually written):

interface UUID {
    /*  making  */
    make(version: number, ...params: any[]): UUID;

    /*  parsing  */
    parse(str: string): UUID;

    /*  formatting  */
    format(type?: string): string;

    /*  formatting (alias)  */
    tostring(type?: string): string;

    /*  importing  */
    import(arr: number[]): UUID;

    /*  exporting  */
    export(): number[];

    /*  byte-wise comparison  */
    compare(other: UUID): boolean;
}

export interface UUIDConstructor {
  /*  default construction  */
  new(): UUID;

  /*  parsing construction  */
  new(uuid: string): UUID;

  /*  making construction  */
  new(version: number): UUID;
  new(version: number, ns: string, data: string): UUID;
}

declare var UUID: UUIDConstructor;
export default UUID;

What's the correct way of exporting the "pure-uuid" module?

3
  • You can not declare a constructor declaration in an interface. Commented Sep 22, 2017 at 15:35
  • Try: export = UUID; Commented Sep 22, 2017 at 15:36
  • @Paleo, using export = UUID; shows error TS2693: 'UUID' only refers to a type, but is being used as a value here. when writing new UUID(4).format(). Commented Sep 22, 2017 at 23:41

3 Answers 3

2

Try with export =:

interface UUID {
  // ...
}

interface UUIDConstructor {
  /*  default construction  */
  new(): UUID;
  // ...
}

declare var tmp: UUIDConstructor;
export = tmp;

See also: The documentation on export =.

Then, the following code:

const UUID = require('pure-uuid')

… must be translated with:

import UUID = require('pure-uuid');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the hint. If I do that, than I am receiving the following error: error TS1192: Module '"../node_modules/pure-uuid/uuid"' has no default export..
@BennyNeugebauer I edited. The equivalent to const UUID = require ... is import * as UUID from ....
I tried switching const to import but TS 3.4 complained. What worked was const package = require("package").default;
0

You can not declare a constructor declaration in an interface.

Typescript is a superset of JavaScript, it includes JavaScript. Therefore, your TypeScript should be simply this:

const UUID = require('pure-uuid');
const id = new UUID(4).format();

1 Comment

When using const UUID = require('pure-uuid'); I am losing the benefit of having types and auto completion based on types from UUID.
0

I'm adding this answer here, because the error in this question may pop up with many JavaScript libraries and this is one of the places where people may come looking. The error message was very confusing to me at first. (I encountered this with the AWS SDK.)

So I think the import that you are looking for is of this format:

import * as UUID from 'pure-uuid'

This imports everything from pure-uuid and puts it in the namespace UUID.

Note, that starting from pure-script version 1.4 the simpler import works as expected:

import UIID from 'pure-uuid'

The error TypeError: foo_bar_1.default is not a constructor is caused by the module being imported not providing the wanted definitions in its "default export".

See the TypeScript documentation on modules. The Mozilla documentation on Javascript import-statement may be easier to skim quickly. The syntax is the same in both languages AFAIK. (The TypeScript documentation states the following "Starting with ECMAScript 2015, JavaScript has a concept of modules. TypeScript shares this concept.")

Comments

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.