When should you use this syntax:
import SomeName = require(“something”)
Instead of:
import SomeName from “something”
Or:
const SomeName = require(“something”]
EDIT: excuse the lack of detail and context, I was too quick in posting this. I am very aware of CommonJS and ESM and was specifically looking for why one would use the import SomeName = require(“something”)
form. I come across it occasionally but never use it myself. For some reason, it was hard to google this, but I’ve now found the answer in the TypeScript docs.
https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
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.
The export = syntax specifies a single object that is exported from the module. This can be a class, interface, namespace, function, or enum.
When exporting a module using export =, TypeScript-specific import module = require("module") must be used to import the module.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments