好像import x = require('x')
是 es6 中的无效语法, typescript 文档中没有明确的解释。
最佳答案
Q1:import … = require(…)
与 const … = require(…)
在运行时(或一旦代码被编译),两种语法之间没有区别,第一个被转换为第二个。
与 import
:
import x = require('x')
此语法特定于 TypeScript。常数
x
是由在导入的包或包中定义的某些类型给出的类型 @types/x
.与
const
:const x = require('x')
这是 JavaScript 中的有效语法,当然也适用于 TypeScript。在 TypeScript 中,常量
x
类型为 any
.Q2:
import … from …
与 import … = require(…)
How about difference between
import x from 'x'
andimport x = require('x')
语法
import … from …
来自 ES6 标准。我建议阅读 this introduction ES6 模块以及如何导入和导出它们。但是,简而言之,语法
import x from 'x'
相当于:import x = require('x').default
(请注意
.default
成员。)如何转换
import … = require(…)
到 ES6 语法ES6 标准规定所有导出的成员都可以导入到单个“命名空间对象模块”中。
那么最接近的标准语法
import x = require('x')
是:import * as x from 'x'
此语法目前适用于 TypeScript 转译,因为代码已转换为
const … = require(…)
.但是:此语法应仅在标准定义的上下文中使用。因为,当你的代码将使用原生版本的 ES6 模块时,您将无法以这种方式导入函数或类 .
关于typescript - typescript 中 `import x = require(' x' )` and ` const x = require ('x' )` 之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52534910/