This might not be possible due to the difference between Types and Values. But thought it worth asking.
interface Entity {
id: string;
code: number;
}
const convert = <E extends Entity>( presentable:E ): Entity => {
// Is it possible to remove keys in 'presentable' that do not exist in `Entity`?
return presentable
}
// should return { id: '10', code: 100 }
const converted1 = convert({ id: '10', code: 100 });
// should return { id: '5', code: 200 }
const converted2 = convert({ id: '5', code: 200, label: 'My label', name: 'My name' });
I want to remove "presentation" properties before sending in a HTTP payload. Type "Entity" can change to different interfaces, so I don't want to check for specific keys.
Maybe there is a way to enforce keys as an argument that don't match both interfaces? But I wouldn't know how to say:
keyof E and not keyof Entity. ?