typescript - 在不同文件中声明具有相同名称的类型时重复标识符
问题描述
我有一个简单的 TypeScript 'snippets' 项目,我遇到了一个问题,即多个 .ts 文件将具有相同的类型名称(例如 Foo)。
//file-a.ts
type Foo = {
}
//file-b.ts
type Foo = {
}
如果我尝试编译它,我会得到:
error TS2300: Duplicate identifier 'Foo'.
这对我来说没有意义——因为这些类型应该特定于模块。
我正在使用 typescript 3.7.5 版,我的 tsconfig.json 具有这些属性(其他所有属性都是默认值):
{
"compilerOptions": {
"target": "es5", /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017', 'ES2018', 'ES2019' or 'ESNEXT'. */
"module": "commonjs", /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */
"outDir": "./dist", /* Redirect output structure to the directory. */
"rootDir": "./src", /* Specify the root directory of input files. Use to control the output directory structure with --outDir. */
"isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */
"strict": true, /* Enable all strict type-checking options. */
"esModuleInterop": true, /* Enables emit interoperability between CommonJS and ES Modules via creation of namespace objects for all imports. Implies 'allowSyntheticDefaultImports'. */
"forceConsistentCasingInFileNames": true /* Disallow inconsistently-cased references to the same file. */
}
}
这是怎么回事?实际代码在这里。
解决方案
export
如果您对TypeScript 文件不执行任何操作,它将在全局范围内编译。要使.ts
文件成为封闭的模块,您export
至少需要做一件事。
推荐阅读
- node.js - 基于另一个集合 mongoose 更新集合上的虚拟字段
- php - Laravel Backpack 中每个 Event Crud 的单独 SMTP
- java - 如何使用 Java 在调用(当前)shell 中运行 shell 命令
- javascript - 如何使复选框在 24 中仅单击一次?
- java - Flink 任务管理器超时
- javascript - esbuild 不捆绑文件
- html - ipad 模式下右侧的空白垂直列/空格
- liquibase - 如何使用 liquibase 从数据库中加载序列 currvalue 和 incremenBy?
- app-inventor - MIT APP Inventor 2 文本框属性为整数
- python-3.x - 释放鼠标后,pygame中的图像立即消失