When I write some code, I have some problems like that:
function getObjectKeys<T extends object>(object: T) {
return Object.keys(object) as (keyof T)[]
}
const props = {
propA: 100,
propB: 'text'
}
const store = { ...props }
getObjectKeys(props).forEach((key) => {
store[key] = props[key]
})
reported some errors:
const store: {
propA: number;
propB: string;
}
Type 'string | number' is not assignable to type 'never'.
Type 'string' is not assignable to type 'never'.
when I write like this:
getObjectKeys(props).forEach((key) => {
if (key === 'propA') {
store[key] = props[key]
} else if (key === 'propB'){
store[key] = props[key]
} else {
store[key] = props[key]
}
})
It can work but not so good. how to solve them?