N
N
Ne7Le4Der2022-03-17 12:45:38
typescript
Ne7Le4Der, 2022-03-17 12:45:38

How to update a property whose name was passed as an argument?

There is an interface

export interface User {
    userID: number,
    role: string,
    name: string,
    active: boolean,
    phone: string
}

I want to do something like this, but this approach does not work
const onUserChange = (user: User, accessor: string, value: string) => {
            user[accessor] = value;
            return user;
    };


Those. update the property whose name was passed as a function argument

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
WbICHA, 2022-03-17
@WblCHA

<K extends keyof User>(user: User, accessor: K, value: User[K]) => {

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question