gpt4 book ai didi

reactjs - 初始化对象变量,错误 : * *'const' declarations must be initialized. **

转载 作者:行者123 更新时间:2023-12-05 01:33:43 24 4
gpt4 key购买 nike

我正在尝试初始化一个对象并为该对象赋值,但我似乎在声明该对象时遇到错误。

Error: 'const' declarations must be initialized.

如何初始化我的对象?

代码:

// Object
export type DateRange = {
date: string;
};

const dateYears = (values: any) => {
if (Array.isArray(values.dateYears)) {

const dateRange: DateRange; // error
values.dateYears.map((item: any) => {
dateRange.createdDate = item.value;
});
}
};

最佳答案

来自 MDN Docs

An initializer for a constant is required. You must specify its value in the same statement in which it's declared. (This makes sense, given that it can't be changed later.)

The const declaration creates a read-only reference to a value. It does not mean the value it holds is immutable—just that the variable identifier cannot be reassigned. For instance, in the case where the content is an object, this means the object's contents (e.g., its properties) can be altered.

因此,您不能只初始化一个const 变量。你必须用一些值来定义它,

const dateRange: DateRange = {};

关于reactjs - 初始化对象变量,错误 : * *'const' declarations must be initialized. **,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64443301/

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