gpt4 book ai didi

Javascript 类型 'false' 无法分配给类型 'string'

转载 作者:行者123 更新时间:2023-12-03 07:24:47 25 4
gpt4 key购买 nike

试图找出声明 event.allDay = false 时我错过了什么。

以下代码工作正常,但 vscode 错误不会消失。

loadFromDevice(year, month, status) {
this.db.dbState().subscribe(res => {
if (res) {
this.db.fetchActivities(year, month, status).subscribe(item => {

// console.log("fetchActivities:")
// console.log(item)

// var entry: {
// allDay: boolean
// }

item.forEach(function (entry) {
var date = new Date();
entry.startTime = new Date(entry.start_datetime);
entry.endTime = new Date(entry.end_datetime);
entry.allDay = false
})

return this.eventSource = item;

})
}
})
}

enter image description here

如果我犯了错误,请感谢任何线索或纠正。谢谢。


解决方案

根据我收到的建议将我的解决方案粘贴到此处。

loadFromDevice(year, month, status) {
this.db.dbState().subscribe(res => {
if (res) {
this.db.fetchActivities(year, month, status).subscribe(data => {

// console.log("Activities->loadFromDevice.fetchActivities:")
// console.log(data)

let cal = [];
data.forEach(function(item) {
cal.push({
title: item.title,
startTime: new Date(item.start_datetime),
endTime: new Date(item.end_datetime),
allDay: (item.all_day == 'true') ? true : false
})

})

console.log("Activities->loadFromDevice.cal:")
console.log(cal)

return this.eventSource = cal;
});
}
});
}

这非常有效!

最佳答案

您不是在编写 JavaScript,而是在编写 TypeScript。

在某个地方(没有提供足够的上下文),这些 entry 对象有一个类型定义,声明 allDay 属性由字符串表示。它可能看起来像这样(但也可能是类型,...而不是接口(interface)):

interface Entry {
startTime: Date;
endTime: Date;
allDay: string;
}

但是,您尝试分配一个 bool false 值(不是字符串),因此 TypeScript 编译器会通知您类型违规。

关于Javascript 类型 'false' 无法分配给类型 'string',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62277068/

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