Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xslt/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
TypeScript:推断抽象方法的类型';实施_Typescript_Typescript Typings_Typescript Generics - Fatal编程技术网

TypeScript:推断抽象方法的类型';实施

TypeScript:推断抽象方法的类型';实施,typescript,typescript-typings,typescript-generics,Typescript,Typescript Typings,Typescript Generics,我希望有一个具有属性的基类,其类型是从方法的返回值推断出来的,该方法由子类实现 因此,基类可以是这样的smg: abstract class MyBase<T> { protected myProperty: T; constructor() { this.myProperty = this.getValueForMyProperty(); } protected abstract getValueForMyProperty():

我希望有一个具有属性的基类,其类型是从方法的返回值推断出来的,该方法由子类实现

因此,基类可以是这样的smg:

abstract class MyBase<T> {
    protected myProperty: T;

    constructor() {
        this.myProperty = this.getValueForMyProperty();
    }

    protected abstract getValueForMyProperty(): T;
}
class MyDescendant extends MyBase<MyDescendant> {
    public getValueForMyProperty() {
        return {
            prop1: "smg"
        };
    }
}
我可以在TypeScript中执行此操作吗?

我有一个解决方案,它几乎就是您想要的。限制是,为了使其工作,
getValueForMyProperty
必须是
public

此设置的关键在于,我们使用
T
来描述对象本身,而不是使用泛型
T
来描述返回类型。这样,我们可以创建如下特定实例:

abstract class MyBase<T> {
    protected myProperty: T;

    constructor() {
        this.myProperty = this.getValueForMyProperty();
    }

    protected abstract getValueForMyProperty(): T;
}
class MyDescendant extends MyBase<MyDescendant> {
    public getValueForMyProperty() {
        return {
            prop1: "smg"
        };
    }
}
Typescript接口仅描述对象的
公共属性
,不能使用
受保护
私有
等修饰符。这就是为什么此设置要求
mydegent
上的
getValueForMyProperty
方法必须是公共的。否则它将无法实现所需的接口

不知何故,我在用
public
方法实现
protectedabstract
方法时没有出错?这似乎是一个错误,所以我也从
MyBase
中删除了
protected

MyBase的类型现在有点混乱,但这没关系,因为这只是一个地方

abstract class MyBase<T extends CanGetProperty> {
    protected myProperty: ReturnType<T['getValueForMyProperty']>;

    constructor() {
        this.myProperty = this.getValueForMyProperty();
    }

    abstract getValueForMyProperty(): ReturnType<T['getValueForMyProperty']>;
}
抽象类MyBase{
受保护的myProperty:ReturnType;
构造函数(){
this.myProperty=this.getValueForMyProperty();
}
抽象getValueForMyProperty():ReturnType;
}

我在中进行了测试,我得到了关于不同类型的后代的返回类型的良好推断。

非常感谢,这对我来说非常完美:)
abstract class MyBase<T extends CanGetProperty> {
    protected myProperty: ReturnType<T['getValueForMyProperty']>;

    constructor() {
        this.myProperty = this.getValueForMyProperty();
    }

    abstract getValueForMyProperty(): ReturnType<T['getValueForMyProperty']>;
}