HardCoreQual
HardCoreQual

Reputation: 391

How extract interface from exist class in typescript

I want extract interface from class so that not duplicate method declaration in class and interface.

typeof MyClass

not extract interface, also I think what I can use simple MyClass intead insterface but MyClass contain information about private methods and params

class MyClass {
  public get() {}
  private secret() {}
}

function run(e: MyClass) {  // need work with MyClassInterface
  e.get();
}

run(new MyClass());

run({
  get: () => {} // require secret method
});

run({
  get:() => {},
  secret: () => {}, // require what secret be private
});

Upvotes: 0

Views: 116

Answers (1)

Wang Weixuan
Wang Weixuan

Reputation: 351

The following type extracts public members from MyClass:

type MyClassInterface = { [k in keyof MyClass]: MyClass[k] };

Reference: Microsoft/TypeScript#471

Upvotes: 2

Related Questions