0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

It is not working. Red color is under [] after equation mark on second line.
But if I change it to const tvDrawerObject: { menuItemRefs: MenuItemRefsProps[]; } = { menuItemRefs: [] }; it looks ok.
Ahhh. I missed that, will edit the answer, we shouldn't try to initialize when specifying a type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.