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?
export = UUID;export = UUID;showserror TS2693: 'UUID' only refers to a type, but is being used as a value here.when writingnew UUID(4).format().