问题表现
在编写ts代码的时候遇到一个问题, 表现为, 如果在某个ts
工程中, 如果多个文件里面没有任何导出export
或者是export default
, 那么这些文件如果有const
或者是let
定义相同的声明都会报错如下
无法重新声明块范围变量
a/a.ts
和 index.ts
和 index2.ts
都没有进行export
任何内容
Typescript 官方解释
Typescript 官方解释
How JavaScript Modules are Defined In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.
Conversely, a file without any top-level import or export declarations is treated as a script whose contents are available in the global scope (and therefore to modules as well).
Modules are executed within their own scope, not in the global scope. This means that variables, functions, classes, etc. declared in a module are not visible outside the module unless they are explicitly exported using one of the export forms. Conversely, to consume a variable, function, class, interface, etc. exported from a different module, it has to be imported using one of the import forms.