导入语句中的解构赋值

Destructuring assignment within import statements

根据这个source在某处项目中看到这种用法的模糊记忆,我很好奇是否有人能够做到以下:

import {map: { series }} from 'contra'

如本 destructuring assignment 概述所述:

The import statement in ES6 behaves similarly to destructuring, but it is important to note that it is not actually destructuring.

导入的工作方式似乎有点不同,也许人们不能期待完全相同的行为,但我无法验证其状态。我正在尝试做的是官方 ECMAScript 6/7 规范的一部分吗?

在尝试回答这个问题时,请包括(或 link)规范中阐明此问题的部分。

您只能在分配 变量时使用解构分配。导入是完全不同的东西,你不能对它们使用解构赋值。

您可以通过直接 require() 调用使用解构赋值(假设您使用的是 Node.js 或 RequireJS):

const {map: { series }} = require('contra')

规范的相关部分是 here

ImportDeclaration

import ImportClause FromClause;

如果您检查 ImportClause,您会发现它就是熟悉的 * as FooDefaultImport{ImportSpecifier, ...} 等。 ,其中 ImportSpecifier 是一个 ImportBinding,它又是一个 BindingIdentifer,这只是一个普通的旧标识符.

MDN 条目介于误导和完全错误之间。它应该说(现在确实说):

The import statement in ES6 has a superficial resemblance to destructuring, but is actually completely unrelated.

最近我在 MDN 中发现越来越多的错误和误导信息。它应该与一粒盐一起服用。权威参考为spec.