'Typescript Override Function for Object [duplicate]

So I am Having this small little function for pinia. Where I do throw in an ID of my Object to filter it (result is of type Task | undefined) I am giving in a Key which should of course be a keyof Task and now I kind of struggle with the value part. I've tried to find out what serves my usecase best, it must be something like a value Of or so but I do currently not find the correct Utility Type (if it even should be one).

changeValue(id: number, key: keyof Task , value: Partial<Task>) {
    const task = this.one(id);
    
    if (task) {
        task[key] = value;
    }
}

Also I have a Second Problem here that task[key] says its of type never can anyone explain why this is of type never?



Solution 1:[1]

You could make your code work simply by leveraging the TypeScript inference with proper control flow:

changeValue(id: number, key: keyof Task , value: Partial<Task>) {
    const task = this.one(id);
    
    if(task === undefined)
        return; // Handle the undefined case
    
    const propToAssign = value[key];

    if(propToAssign === undefined)
        return; // Handle the undefined case

    task[key] = propToAssign;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Guerric P