A possible solution is the function above:
function createExampleInterface(sourceObject: ExampleInterface): ExampleInterface
{
const emptyExampleInterface: ExampleInterface = {
property1: '',
property2: ''
};
const interfaceProperties = Object.keys(emptyExampleInterface);
const targetObject: ExampleInterface = Object.assign({}, sourceObject) ;
for (let property of Object.keys(targetObject)) {
if (interfaceProperties.indexOf(property) < 0) {
delete targetObject[property];
}
}
return targetObject;
}
Example using the function:
const objA = {
property1: 'Property 1',
property2: 'Property 2',
property3: 'Property 3'
}
const objB: ExampleInterface = createExampleInterface(objA);
console.log(objB);
Try it in https://stackblitz.com/edit/typescript-rjgcjp