mikemaccana
mikemaccana

Reputation: 123088

TypeScript way to do multiple imports at once?

Since inbuilt modules in TypeScript will be strictly typed if import is used, is there a way to do the equivalent of the node/CommonJS 'single require'?

Eg, rather than:

const x = require("x"),
    y = require("y")

Do something like:

import x = require("x"),
    y = require("y")

The above is an error using ES2017 target for TSC.

Upvotes: 0

Views: 197

Answers (1)

unional
unional

Reputation: 15589

Nope.

There is no syntax to support this.

import x = require('x'), y = require('y') suggests import is somethin like var, let or const, but it is not. It carries special meaning in the language.

I think that's why they make the choice of not including it into the language.

IMO, it is cleaner and easier to maintain when they are separated into separate lines.

It's the same arguments of var x = 1, y = 2; vs var x = 1;\n var y = 2;

By the way, starting from ES2015, you will use the import ... from ... syntax instead. Unless you are importing commonJS library.

In ES2015, there is also no syntax of importing multiple modules "at once" either.

And targeting ES2017 would not work with import x = require('x') syntax.

You have to do import * as x from 'x' which gets into interop and it has other problems.

You can learn more from here: https://github.com/Microsoft/TypeScript/issues/16093

UPDATE: with [email protected] released, you can now do import EditableElement from 'Transformer' directly.

Turn on esModuleInterop in your tsconfig.json

Upvotes: 3

Related Questions