我正在将Node + ES6项目转换为TypeScript。我的目标是ES6(因为我正在运行Node 7.x)并使用Map。
运行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"
]
}
答案 0 :(得分:1)
不确定var {...
构造是否有效。 var
使变量作用域类型为全局。
如果您在 actions.ts :
中使用该怎么办?const moment = require("moment")
import calendar = require("./calendar");
// console.log(calendar.events)