我正在将 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 导入的上下文中。使用标准const
在 require 声明中更严重地破坏了这一点。
日历.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
使变量具有全局作用域。
如果您在 actions.ts 中使用会怎样:
const moment = require("moment")
import calendar = require("./calendar");
// console.log(calendar.events)
?
我是一名优秀的程序员,十分优秀!