Vladimír
Vladimír

Reputation: 759

How to initialize object with property of array

I have this code:

interface MenuItemRefsProps {
    current: React.Component;
    tag: number;
    route: string;
};

let tvDrawerObject: {
    menuItemRefs: MenuItemRefsProps[];
};
tvDrawerObject = { menuItemRefs: [] };

But my eslint complains, that

'tvDrawerObject' is never reassigned. Use 'const' instead.

If I use const for 'tvDrawerObject' I need to initialize it. I don't know how to do it. I tried something like this:

const tvDrawerObject: {
    menuItemRefs: MenuItemRefsProps[] = [];
};

Then another complain arise:

A type literal property cannot have an initializer.

Upvotes: 0

Views: 722

Answers (1)

Terrance00
Terrance00

Reputation: 1678

What you should do is, initialize the const right away:

const tvDrawerObject: {
    menuItemRefs: MenuItemRefsProps[]
} = { menuItemRefs: [] };

The reason for this is because const's should not be able to change after initialization.

Upvotes: 1

Related Questions