Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript multiple imports

I want to import 'express' module to my app.

as Mozilla says, we can use this code write below:

import { Application }, * as Express from 'express'

but when I use it in typescript and VSCode it shows error so I forced to use this code:

import * as Express from 'express'
import { Application } from 'express'

how can I solve the problem?

like image 638
Hossain Khademian Avatar asked Jul 21 '17 07:07

Hossain Khademian


2 Answers

Your code :

import { Application }, * as Express from 'express'

Is wrong. Fortunately you are using TypeScript and its preventing a bug. The correct syntax is:

import * as Express from 'express'
import { Application } from 'express'

Which you have already figured out. You cannot have member imports and * imports in the same line. The MDN docs also reflect that https://developer.mozilla.org/en/docs/web/javascript/reference/statements/import

like image 148
basarat Avatar answered Sep 28 '22 06:09

basarat


If I have interpreted TypeScript correctly then importing modules using:

import * as Express from 'express'

Will create a Namespace that you can reference all of the different methods/components inside using syntax such as:

Express.Application

Imports with

import { Application } from 'express'

Will instead only import the Application member of Express, which then is referensed as its own class.

If you don't need everything from Express, then the first import is unnecessary, you can instead chain your dependencies with

import { Application, 'Member1', 'Member2' } from 'express'
like image 28
Felix Nordén Avatar answered Sep 28 '22 06:09

Felix Nordén