I have an object of type Record<string, unknown> and I am using .reduce() to iterate over all the properties.
Now I want to perform an action on each of the property values that are strings.
But I can't figure out how to narrow the property value from unknown to string.
My code looks something like this:
const obj: Record<string, unknown> = {
foo: 'bar',
}
Object.keys(obj).reduce((result, key) => {
if (typeof obj[key] === 'string') {
const stringValue = obj[key]; // 'stringValue' should be 'string' type, but is currently 'unknown'
// Do something here with a string
}
return {
...result,
};
}, {});
Here is the TypeScript playground link.