Tuesday, 11 April 2017

node + TypeScript: Cannot redeclare block-scoped variable 'events'

I'm in the process of converting a Node + ES6 project to TypeScript. I'm aiming for ES6 (as I'm running Node 7.x) and using Map.

Running tsc -p returns:

  • `src/actions.ts(3,9): error TS2451: Cannot redeclare block-scoped variable 'events' - it's not clear
  • 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'.

It's not clear why these are duplicate identifiers or are being tagged as re-declared.

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"
  ]
}



via elithrar

No comments:

Post a Comment