0

Unfortunatelly I can't find answer or solution. So: if I use a static string as key, it works - but if I use a variable that gives exactly THE SAME string, Angular does not accept it.

The error message is weird: "Element implicitly has an 'any' type because expression of type 'string' can't be used to index type". (Of course this is NOT true, plus that is not index but key. Anyway.)

enter image description here

Thank for any help.

2 Answers 2

1

you should do something like

this.rendel[adat.kulcs as keyof typeof this.rendel] = adat.v
Sign up to request clarification or add additional context in comments.

2 Comments

Wow! This is working. I check once more. Thank you very much. :)
Please could you tell me is there a solution to declare a class with keys of the 'rendel' object, and then import and use that?
1

Obviously, It doesn't allow strings.

You should say that it's not an only string, it's one of the keys of the rendel object, so you should create a new interface and update the addItem function as the following

interface Rendel {
    alap: string,
    tipus: string,
    felfet: string
}


addItem(adat: {v: string, kulcs: keyof Rendel}) {
        this.rendel[adat.kulcs] = adat.v
}

1 Comment

I tried this, but I got error message: Property 'addItem' does not exist on type 'AppComponent'.. Weird.

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.