javascript - Typescript: Create interface based on a class -
i got class called myclass. has bunch of properties. want create interface contains options of class. many options have same name , typing myclass, not all.
edit: options properties optional.
the goal duplicate least possible.
right now, i'm using dummy object avoid code duplication. there cleaner solution 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; }
many options have same name , typing myclass, not all.
if case, there's not option.
if want have exactly same members, , class doesn't have private
or protected
properties/methods, can write
interface myclassoptions extends myclass { extraprop: string; }
one thing make base class options want share, make interface there "real" class use being derivative of base class.
Comments
Post a Comment