I'm facing a challenge while working with the node_module called uuid-js
in TypeScript. After installing both the module and its typings, I am unsure how to properly import the module.
Question: What is the correct way to import the module?
The function I intend to use is uuid.create
, which is defined in the typings of uuid-js
as follows:
export = uuid;
declare class uuid {
equals(uuid: uuid): boolean;
...
static create(version?: number): uuid;
...
}
The TypeScript documentation mentions that:
When using export = to import a module, you must make use of TypeScript-specific import module = require("module") syntax for importing the module.
Based on this information, it seems like I should do the following:
import UUID = require('uuid-js');
Which would then compile to this:
const UUID = require("uuid-js")
Is there a specific reason why I should use import instead of const in my code? Could I not just directly utilize the compiled code?
Using require altogether feels somewhat peculiar. Perhaps, it might be better to avoid using the typings or exploring an alternative approach...?
So, what exactly sets apart the usage of:
import UUID = require('uuid-js');
From
const UUID = require("uuid-js")
I did attempt a more conventional import method recommended by Madara Uchiha, but encountered errors.
For instance, using import * as UUID from 'uuid-js'
led to:
error TS2497: Module '".../node_modules/@types/uuid-js/index"' resolves to a non-module entity and cannot be imported using this construct.
Similarly, trying import UUID from 'uuid-js';
resulted in:
error TS1192: Module '".../node_modules/@types/uuid-js/index"' has no default export.