首页 > 解决方案 > 如何排除 getter 和 setter?

问题描述

考虑下面的类Foo

class SomeClass {

}
class Foo {
    readonly foo: string
    readonly baz?:string
    private someclass: SomeClass
    set bar(v:boolean){
       // th    is.foo = v
    }


    getFoo() {

    }

    constructor(json: FooLike) {
        this.foo = json.foo
        this.baz = json.baz
        this.someclass = json.someclass
    }
}

我的目标是获得类似这样的界面

interface FooLike {
    readonly foo: string,
    readonly baz?:string,
    someclass: SomeClass,
}

如何删除所有方法、getter 和 setter?

我试过这个:

export type NonFunctionPropertyNames<T> = {
    // tslint:disable-next-line:ban-types
    [K in keyof T]: T[K] extends Function ? never : K;
}[keyof T];
export type NonFunctionProperties<T> = Pick<T, NonFunctionPropertyNames<T>>;


    let z: NonFunctionProperties<Foo>

但这不会删除 setter/getter

标签: javascripttypescriptgenericstypescript-generics

解决方案


推荐阅读