最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Typescript multiple imports - Stack Overflow

matteradmin3PV0评论

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?

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?

Share asked Jul 21, 2017 at 7:19 Hossain KhademianHossain Khademian 1,7064 gold badges19 silver badges29 bronze badges 1
  • What happens if you rearrange it? * as Express should be first. – Win Commented Jul 21, 2017 at 7:35
Add a ment  | 

2 Answers 2

Reset to default 5

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/en/docs/web/javascript/reference/statements/import

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/ponents 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'
Post a comment

comment list (0)

  1. No comments so far