gpt4 book ai didi

node.js - Node + typescript : Cannot redeclare block-scoped variable 'events'

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:24 25 4
gpt4 key购买 nike

我正在将 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)

关于node.js - Node + typescript : Cannot redeclare block-scoped variable 'events' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43361695/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com