抽象类中的keyof this

时间:2021-04-22 03:40:52

标签: javascript typescript visual-studio-code entity-component-system

我如何解决这个问题以在我实现的抽象 getter 中获取类型建议 get Getter2();

我在这里做了一个游乐场:link to ts playground

type propsSettings = {name:string,desc?:string,input?:'input'|'select'|'switch'}

export abstract class Entity<T> {
    abstract get Getter1():() => any;
    abstract get Getter2(): Partial<Record<keyof this, propsSettings>>;
    _myprop0: boolean = true;
    constructor() {
        Object.defineProperty(this, 'Getter1', {
            enumerable: false,
            writable: false,
            configurable: true,
        });
        Object.defineProperty(this, 'Getter2', {
            enumerable: false,
            writable: false,
            configurable: true,
        });
    }
}


class Foo extends Entity<any> {
    _myprop1: boolean = false;
    _myprop2: string = 'hello friend'
    /** @implements of a renderer */
    get Getter1() {
        return myGetter1
    }
    /** Implement description of public props of the child class , should allow replicate all public keyprops */
    get Getter2() { // :Foo['Getter2'] as type also not work !
        return {
            _myprop1: { name: 'MyPropOne', input: 'switch' },
            _myprop2: { name: 'MyPropTwo', input: 'select' },
        };
    }
}
const myGetter1 = ()=>{};



// test 
const test = new Foo();
test.Getter2._myprop1;
Object.entries(test.Getter2).map((entrie)=>{
    const ekey = entrie[0] as keyof Foo;
    const esetting = entrie[1];
    const value = test[ekey]
})

存在这个问题,我无法获得道具建议,因为 ts lint me 错误

Property 'Getter2' in type 'Foo' is not assignable to the same property in base type 'Entity<any>'.
  Type '{ _myprop1: { name: string; input: string; }; }' is not assignable to type 'Partial<Record<keyof this, propsSettings>>'.(2416)

enter image description here

感谢帮助

0 个答案:

没有答案
相关问题