简体   繁体   English

如何不导入与 TypeScript 同名的类型/接口/类

[英]How to not import types/interfaces/classes with the same name with TypeScript

I used to have a simple filestructure of 5 files in one folder:我曾经在一个文件夹中有 5 个文件的简单文件结构:

  • classes.ts类.ts
  • hello.ts你好.ts
  • types.ts类型.ts
  • functions.ts函数.ts
  • tsconfig.json tsconfig.json

tsconfig.json: tsconfig.json:


    {
      "compilerOptions": {
        "module": "commonjs",
        "target": "es2015",
        "sourceMap": false,
        "strict": true,
        "strictPropertyInitialization": false
      },
      "files": [
        "classes.ts",
        "hello.ts",
        "functions.ts",
        "types.ts"
      ],
      "exclude": [
        "node_modules"
      ]
    }

However, as time passes I started to have type/interface/class names collision.然而,随着时间的推移,我开始发生类型/接口/类名冲突。 The question is: how do I prevent classes.ts from knowing about a particular type (or all of them) from hello.ts?问题是:如何防止 classes.ts 从 hello.ts 了解特定类型(或所有类型)? It seems like they were implicitly imported which isn't the desired behaviour for me.似乎它们是隐式导入的,这不是我想要的行为。

I tried separating them into different folders with a different tsconfig.json per folder but then.ts files are not compiled all at once, just folder-by-folder.我尝试将它们分成不同的文件夹,每个文件夹使用不同的 tsconfig.json 但是 then.ts 文件不会一次全部编译,只是逐个文件夹编译。

TypeScript distinguishes between two types of files: TypeScript 区分两种类型的文件:

  • modules , which might be in CommonJS or ES dialects modules ,可能是 CommonJS 或 ES 方言
  • scripts , which do not use "import", "export", "module.exports", or any other module machinery, and consequently everything defined in a script goes into the global namespace.不使用“import”、“export”、“module.exports”或任何其他模块机制的脚本,因此脚本中定义的所有内容都会进入全局命名空间。

As in the "Modules" Handbook docs , emphasis mine:正如在“模块”手册文档中一样,强调我的:

In TypeScript, just as in ECMAScript 2015, any file containing a top-level import or export is considered a module.在 TypeScript 中,就像在 ECMAScript 2015 中一样,任何包含顶级importexport的文件都被视为模块。

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).相反,没有任何顶级导入或导出声明的文件被视为脚本,其内容在全局 scope 中可用(因此也可用于模块)。

Despite your use of the term "import types", you clarified in the comments that these are scripts, such that these types are not imported as they would be in modules: Everything defined in the global namespaces is accessible to everything else defined in the global namespace.尽管您使用了术语“导入类型”,但您在注释中澄清了这些是脚本,因此这些类型不会像在模块中那样导入:全局命名空间中定义的所有内容都可以访问全局命名空间中定义的所有其他内容命名空间。 As in this Handbook reference example advocating for namespaces , emphasis mine:正如本手册中提倡使用命名空间的参考示例一样,强调我的:

As we add more validators, we're going to want to have some kind of organization scheme so that we can keep track of our types and not worry about name collisions with other objects.随着我们添加更多验证器,我们将需要某种组织方案,以便我们可以跟踪我们的类型,而不必担心与其他对象的名称冲突。 Instead of putting lots of different names into the global namespace, let's wrap up our objects into a namespace.与其将许多不同的名称放入全局命名空间,不如将我们的对象包装到一个命名空间中。

That Handbook reference page suggests one solution, which is to use the namespace keyword to give potential collisions more specific names;该手册参考页提出了一种解决方案,即使用namespace关键字为潜在的冲突提供更具体的名称; you can then use aliases to reduce the length of the names to type.然后,您可以使用别名来减少要键入的名称的长度。 However, you might also consider using explicit modules , which were designed with modularity and name collision avoidance in mind.但是,您也可以考虑使用显式 模块,这些模块的设计考虑了模块化和避免名称冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM