For instance:
type T = {
prop1: boolean;
prop2: number;
prop3: string;
};
const obj1 = {
prop1: true,
prop2: 1,
prop3: 'hello',
prop4: false,
prop5: 'world'
}
const obj2: T = obj1 as T; // the outcome is not as anticipated
Is there a way to dynamically assign obj2 to only include properties from obj1 that match type T?
In other words, retain matching properties and discard others. The desired result in this scenario would be equivalent to
const obj2: T = {
prop1: obj1.prop1,
prop2: obj1.prop2,
prop3: obj1.prop3
}
This should happen dynamically considering that type T can change.