代码之家  ›  专栏  ›  技术社区  ›  Radex

ES6中的Understanding import

  •  0
  • Radex  · 技术社区  · 8 年前

    import type { a, b, c, d } from 'types'

    import { a, b, c, d } from 'types' 你能解释一下吗?谢谢

    import type { a, b, c, d } from 'types'// not sure what it does
    import { a, b, c, d } from 'types' // I am familar with this
    3 回复  |  直到 8 年前
        1
  •  3
  •   apsillers    8 年前

    这不是普通的JavaScript import

    我从Flow项目中找到了一篇题为 Announcing Import Type . 我不知道流,但它看起来像是JavaScript的严格类型超集。这个 import type 语句是如何在不导入类本身的情况下导入有关类的类型信息。它们给出了一个示例,您可能希望在函数中声明stirctly类型的形式参数,并需要导入适当的类型:

    import type {Crayon, Marker} from 'WritingUtensils';
    module.exports = function junkDrawer(x: Crayon, y: Marker): void {}
    
        2
  •  1
  •   sahil gupta    8 年前

    它正在从文件中导入类型定义。

    // Here you are importing the actual method, variable from the file.
    import xyz from 'abc';`
    
    // Here you are importing the type defination of xyz
    import type { xyz } from 'abc';
    

    let a: xyz = new xyz();

        3
  •  -2
  •   Salketer    8 年前

    从MDN来看,虽然OP中缺失的comas很奇怪:

    导入默认值:可以有默认导出(无论是否 然后用于导入此类默认值。

    最简单的版本直接导入默认值:

    import myDefault from '/modules/my-module.js';

    也可以 将默认语法与上述语法(命名空间导入或 命名导入)。在这种情况下,默认导入必须为 首先声明。例如:

    import myDefault, {foo, bar} from '/modules/my-module.js'; // specific, named imports

    换句话说,这将导入默认值作为myDefault,然后导入命名的导出。