【问题标题】:Duplicate identifier when declaring types with same name in different files在不同文件中声明具有相同名称的类型时标识符重复
【发布时间】:2020-02-18 11:12:02
【问题描述】:

我有一个简单的 TypeScript 'sn-ps' 项目,我遇到了一个问题,即多个 .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. */
  }
}

这里发生了什么? The actual code here.

【问题讨论】:

    标签: typescript


    【解决方案1】:

    如果您没有 export TypeScript 文件中的任何内容,它将在全局范围内编译。要使.ts 文件成为封闭模块,您至少需要export 做一件事。

    【讨论】:

    • 好的,谢谢。我讨厌它。是否有可以防止这种行为的配置项?
    猜你喜欢
    • 1970-01-01
    • 2018-01-12
    • 2013-04-30
    • 2014-08-12
    • 2012-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-19
    相关资源
    最近更新 更多