I am trying to initialize an object and assigned a value to that object but I seem to be getting an error when I declare the object.
Error: 'const' declarations must be initialized.
How do I initialize my object?
Code:
// 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;
});
}
};
From 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.)
Theconstdeclaration 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.
So, You cant just initialize a const variable. You have to define it with some value,
const dateRange: DateRange = {};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With