在不同文件中声明相同名称的类型时,标识符重复

时间:2020-02-17 23:50:09

标签: typescript

我有一个简单的TypeScript'snippets'项目,并且遇到了一个问题,即多个.ts文件将具有相同的类型名称(例如Foo)。

//file-a.ts

type Foo = {

}
//file-b.ts

type Foo = {

}

如果我尝试编译,则会得到:

 error TS2300: Duplicate identifier 'Foo'.

这对我来说没有意义-因为这些类型应特定于模块。

我使用的打字稿版本为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.

1 个答案:

答案 0 :(得分:1)

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

相关问题