TypeScript:嵌套的npm包导致错误(重复标识符)

时间:2017-03-22 07:45:59

标签: typescript npm

我有一个项目文件夹,其根级别为node_modules,另一个项目文件夹位于名为functions的子文件夹中。像这样,

├── functions
│   ├── index.js
│   ├── index.js.map
│   ├── index.ts
│   ├── package.json
│   ├── node_modules
│   └── tsconfig.json
├── package.json
└── node_modules

我想编译这个functions文件夹中的typescript文件,但它会不断查看根node_modules并给我错误,

tsc --project functions
functions/node_modules/@types/node/index.d.ts(60,13): error TS2451: Cannot redeclare block-scoped variable 'global'.
functions/node_modules/@types/node/index.d.ts(84,13): error TS2300: Duplicate identifier 'require'.
node_modules/@types/react-native/index.d.ts(8365,11): error TS2451: Cannot redeclare block-scoped variable 'global'.
node_modules/@types/react-native/index.d.ts(8366,14): error TS2300: Duplicate identifier 'require'.

如何告诉打字稿忘记上层node_modules或以其他方式解决此问题?

tsconfig.json:

{
  "compilerOptions": {
    "target": "es2015",
    "module": "CommonJS",
    "outDir": ".",
    "rootDir": ".",
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": true,
    "experimentalDecorators": true,
    "preserveConstEnums": true,
    "allowJs": true,
    "sourceMap": true
  },
  "filesGlob": [
    "./**/*.ts"
  ],
  "exclude": [
    "node_modules"
  ],
  "compileOnSave": false
}

1 个答案:

答案 0 :(得分:0)

{
  "compilerOptions": {
    "typeRoots": [
      "./node_modules/@types"
    ]
  }
}

此解决方案对我有用 我在这里找到答案 How to block typescript 2.0 from walking up all parent directories while resolving modules?