I got a class called MyClass. It has a bunch of properties. I want to create an interface that contains the options of that class. Many options have the same name and typing than MyClass, but not all.
Edit: Most options properties are optional.
The goal is to duplicate the least possible.
Right now, I'm using a dummy object to avoid code duplication. Is there a cleaner solution than this?
class MyClass {
callback:(p0:number,p1:string,p2:string[]) => number[];
myAttr:number;
composed:string;
constructor(options:MyClassOptions){
if(options.callback !== undefined)
this.callback = options.callback;
if(options.myAttr !== undefined)
this.myAttr = options.myAttr;
if(options.composedP1 !== undefined && options.composedP2 !== undefined)
this.composed = options.composedP1 + options.composedP2;
}
}
var dummy = <MyClass>null;
interface MyClassOptions {
callback?:typeof dummy.callback;
myAttr?:typeof dummy.myAttr;
composedP1:string;
composedP2:string;
}