I have a type, e.g.:
type Output = {
fieldA?: string;
fieldB?: string;
}
And I have an object, e.g.:
const input = { fieldA: "A", fieldB: "B", other: "C" };
Now I want to map the value to another object of type Output
, so I want to loop through the object keys and assign only when the key is a valid property of type Output
, e.g.:
let output: Output = {};
Object.keys(input).forEach(k => {
if (k is keyof Output) {
output[k] = input[k];
}
});
How do you achieve the above?