I'm defining a typescript class with constructor parameters defined by an interface, and it restricts the properties to only those that are defined.
The following code snippet works as intended, however, is there a way to reduce the code so that it isn't so repetitive? Each property is mentioned 4 times, there must be a better way.
interface MyInterface {
property1: string;
property2: boolean;
property3: number;
}
class MyClass {
property1: string;
property2: boolean;
property3: number;
constructor(parameters: MyInterface) {
this.property1 = parameters.property1;
this.property2 = parameters.property2;
this.property3 = parameters.property3;
}
}
const example = new MyClass({property1: "Property 1", property2: true, property3: 3, extraProperty: "Shouldn't exist"});
console.log(example);
EDIT: I also need to restrict the properties at run-time from objects with additional unknown properties.
