节点+ TypeScript:无法重新声明块范围variables“事件”

我正在将Node + ES6项目转换为TypeScript。 我瞄准ES6(因为我运行节点7.x)和使用地图。

运行tsc -p返回:

  • src/actions.ts(3,9): error TS2451: Cannot redeclare block-scoped variable 'events'
  • src/calendar.ts(5,10): error TS2300: Duplicate identifier 'fetchEvents'.
  • src/index.ts(3,5): error TS2403: Subsequent variable declarations must have the same type. Variable 'actions' must be of type 'Map<any, any>', but here has type 'any'.

不清楚为什么这些是重复的标识符或被标记为重新声明,特别是在Node的require / module导入的情况下。 在require语句中使用标准const更加严重。

calendar.ts

 const { rp } = require("request-promise") var events = <any> {} // both are exported via module.exports = { events, fetchEvents } function fetchEvents(key: string, url: string, options: object) { ... 

actions.ts

 const moment = require("moment") var { events, fetchEvents } = require("./calendar") var actions = new Map() 

tsconfig.json

 { "compilerOptions": { "target": "es6", "outDir": "./built", "declaration": true, "rootDir": ".", "baseUrl" : "./packages", "experimentalDecorators": true, "moduleResolution": "node", "noImplicitAny": false, "strictNullChecks": true, "noImplicitReturns": true, "noImplicitThis": true }, "include": [ "src/**/*" ], "exclude": [ "dist", "node_modules", ".vscode" ] } 

不知道var {...结构是否有效。 var使全局范围variables的范围。

如果你在actions.ts中使用了什么:

 const moment = require("moment") import calendar = require("./calendar"); // console.log(calendar.events)