代码之家  ›  专栏  ›  技术社区  ›  dwjohnston

在不同文件中声明同名类型时出现重复标识符

  •  0
  • dwjohnston  · 技术社区  · 6 年前

    //file-a.ts
    
    type Foo = {
    
    }
    
    //file-b.ts
    
    type Foo = {
    
    }
    

    如果我试着编译这个,我会得到:

     error TS2300: Duplicate identifier 'Foo'.
    

    我使用的是TypeScript3.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.

    1 回复  |  直到 6 年前
        1
  •  1
  •   Josh Wulf    6 年前

    如果你没有 export 任何来自TypeScript文件的内容,都会在全局范围内编译。使 .ts 出口

    推荐文章