const obj = {
a: 1,
b: 'foo',
};
for (const k of (Object.keys(obj) as (keyof typeof obj)[])) {
obj[k] = obj[k];
}
I'm getting:
Type 'string | number' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.
obj has different value types for each key and I want to maintain the value type for each key. In other words, I want obj.a to remain number and obj.b to remain string. I'm assuming this error is occurring because TS expects the type of obj[k] to be string | number.
Is there a way to make TS maintain the correct value type for each key?
objcould have properties of multiple types at runtime.Object.assign. Like this:Object.assign(obj, {a:20});OrObject.assign(obj, {a:20, b:"another foo"});