ES6/Typescript import: import * and names on a single line

前端 未结 1 2018
天命终不由人
天命终不由人 2021-02-20 15:58

How can I write this on 1 line?

import * as Express  from \'express\';
import { Application, NextFunction, Request, Response } from \'express\';
<
相关标签:
1条回答
  • 2021-02-20 16:44

    import and export have restricted syntax that allows to statically analyze them:

    As listed in the documentation:

    import defaultMember from "module-name";

    import * as name from "module-name";

    import { member } from "module-name";

    import { member as alias } from "module-name";

    import { member1 , member2 } from "module-name";

    import { member1 , member2 as alias2 , [...] } from "module-name";

    import defaultMember, { member [ , [...] ] } from "module-name";

    import defaultMember, * as name from "module-name";

    import "module-name";

    As it can be seen, there's no import * as name, { member } from "module-name", so it isn't supported.

    The reason why it isn't supported is because import * as name, { member } from "module-name" are interchangeable. It's either importing members one by one or as name namespace.

    If both should be used for some reason, it should be:

    import * as Express from 'express';
    import { Application, NextFunction, Request, Response } from 'express';
    

    Or if exports are real variables and not types/interfaces, it can be:

    import * as Express from 'express';
    const { Application, NextFunction, Request, Response } = Express;
    
    0 讨论(0)
提交回复
热议问题