on-internet
← Community

Extract public interface of a class

Stack Overflow ·

Is there a mapped type that lets you extract the public properties and methods of a TypeScript class into an interface? It would look like this: class Foo { private x: number; public y: number; bar(a: number) {} private baz() {} } type FooPublic = PublicInterface<Foo>; In this example, FooPublic would have the property y and the method bar(), but would not have private x or private baz(). The closest I have come is: type PublicInterface<T> = {[K in keyof T]: T[K]}; However, this doesn't appear to correctly type the methods. I get no error if I say: const example: PublicInterface<Foo> = { y: 5,

Comments

Sign in or sign up to comment and upvote.

No comments yet.