import xxx = require('package');
is the old typescript syntax for importing modules, it is recommended you switch to the new ES standard import syntax. This might not always be possible if the module does not support it:
Both CommonJS and AMD generally have the concept of an exports object which contains all exports from a module.
They also support replacing the exports object with a custom single object. Default exports are meant to act as a replacement for this behavior; however, the two are incompatible. TypeScript supports export = to model the traditional CommonJS and AMD workflow.
When exporting a module using export =
, TypeScript-specific import module = require("module")
must be used to import the module
If you are deciding between import xxx = require('package')
and const xxx = require('package')
, I would definitely use the import
version.
The import
version triggers the typescript machinery to resolve the module and to correctly type the import variable (taking advantage of any module definitions you have for that module).
The const
version is just a function call to a function defined in the node definitions:
declare var require: NodeRequire;
interface NodeRequire extends NodeRequireFunction { /*...*/ }
interface NodeRequireFunction {
(id: string): any; // just returns any.
}
As you can see this function returns any
for any given module name, so in effect the variable holding the imports will be of type any
and thus you will not benefit from any types you might have installed for the module.