TypeScript: import = require vs import from

Otto

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.

Otto

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.

edited at
0

Comments

0 comments
Login to comment

Related

Typescript import/as vs import/require?

Import vs Require in Typescript

Module vs Namespace - Import vs Require Typescript

`from ... import` vs `import .`

TypeScript var vs import

Require, Import, Require Import

Recursive import: 'import' vs. 'from ... import ...'

WebStorm Auto import - "require" instead of "import from"

nodejs convert from import to require

Make typescript to output "require" instead of "import"

Typescript : require statement not part of an import statement

Webpack / typescript require works but import doesn't

Frequent issue with TypeScript and preferring import over require

Typescript: import * as moment from 'moment' vs. import moment from 'moment'

Trouble using require vs import React Native

Jest mocking default exports - require vs import

Exporting mutable values: import vs require

import werkzeug VS from werkzeug import security

require vs load vs include vs import in Racket

Nodejs require()(require()) to import

How to convert Electron files from 'require' to 'import' for use with TypeScript? Angular2+

Visual Studio ReSharper - Typescript automatic import class declared in external module - Use from instead of require

Difference between import http = require('http'); and import * as http from 'http';?

Typescript: Import from nested modules

Import html from typescript with webpack

require() equivalent if import { Something } from 'somewhere'

ES6 - Convert from 'require' to 'import'

Typescript - Import vs "declare var" with type definitions

Use vs Import vs Require vs Require-extension in Chicken Scheme