简体   繁体   English

如何在TypeScript中键入类的函数的返回值作为其类的属性的值?

[英]How to type a class' function's return as the value of its class' attribute in TypeScript?

Provided i have a Class Factory who takes in another class Product (or its subclass) as an argument and has methods that return instantiated Products will a lot of extra done to them, like so: 假设我有一个类Factory ,该类Factory将另一个类Product (或其子类)作为参数,并且具有返回实例化Products的方法,则将对它们做很多额外的工作,例如:

abstract class Product {}
class Wheel extends Product {}
class Roof extends Product {
    is_asbestos(){ return false } }
type ProductClass = {new (): Product}

class Factory {
    public product_cls: ProductClass;
    constructor(product_cls: ProductClass, private regulation_code?: number) {
        this.product_cls = product_cls;
    }
    build_for_france(){
        return new this.product_cls();
    }
    build_for_britain(){
        return new this.product_cls();
    }
}

let wheel_factory = new Factory(Wheel);
let roof_factory = new Factory(Roof, 123);
let new_roof = roof_factory.build_for_france();

new_roof.is_asbestos();  // error

Calling the is_asbestos method gives me this error. 调用is_asbestos方法会给我这个错误。

Property 'is_asbestos' does not exist on type 'Product'. 属性“ is_asbestos”在“产品”类型上不存在。

So, how do i tell TypeScript that the return value of that function is an instance of the value of product_cls , which is a class. 因此,我如何告诉TypeScript该函数的返回值是product_cls值的实例,后者是一个类。

Now, i can do the below way and it works, but doing that at every function call is silly. 现在,我可以按照以下方式进行操作,并且可以正常工作,但是在每个函数调用中这样做都是很愚蠢的。

let new_roof = <Roof>roof_factory.build_for_france();
new_roof.is_asbestos();

Doing the following gives me a syntax error. 执行以下操作会给我带来语法错误。

build_for_france(){
    let french_product: this.product_cls = new this.product_cls();
    return french_product;
}

Finally, I'm using Typescript 2.0.10. 最后,我正在使用Typescript 2.0.10。 I do not mind upgrading the TypeScript version. 我不介意升级TypeScript版本。

You must use generics to specify which type your factory constructs. 您必须使用泛型来指定工厂构造的类型。

abstract class Product {}
class Wheel extends Product {}
class Roof extends Product {
    is_asbestos(){ return false } }
type ProductClass<T> = {new (): T}

class Factory<T extends Product> {
    public product_cls: ProductClass<T>;
    constructor(product_cls: ProductClass<T>, private regulation_code?: number) {
        this.product_cls = product_cls;
    }
    build_for_france(): T {
        return new this.product_cls();
    }
    build_for_britain(): T {
        return new this.product_cls();
    }
}

let wheel_factory = new Factory<Wheel>(Wheel);
let roof_factory = new Factory<Roof>(Roof, 123);
let new_roof = roof_factory.build_for_france();

new_roof.is_asbestos();  // ok

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在TypeScript中将class属性设置为Moment类型? - How to set a class attribute in TypeScript as a Moment type? 根据 Typescript Angular 中的类型为类实例属性分配一个值 - Assign class instance property a value based on its type in Typescript Angular typescript:如何注释类型是 class(而不是 class 实例)? - typescript: How to annotate a type that's a class (instead of a class instance)? 如何在typescript中动态定义class的属性及其数据类型 - How to define class with properties and its data type dynamically in typescript 如何告诉Typescript在这种情况下*函数的返回类型永远不会为null - How to tell Typescript that *in this instance* a function's return type is never null 在 Typescript 中实现抽象 class 的 class 的动态返回类型 - Dynamic return type for a class implementing an abstract class in Typescript 如何将 function 或其返回值从 typescript (.ts) 导入 javascript (.js) - How to import a function or its return value from typescript (.ts) to javascript (.js) 打字稿:从扩展类调用超级方法会产生类型错误 - (中间值)不是函数 - Typescript: calling super method from extended class gives type error - (intermediate value) is not a function 从另一个方法的内部函数访问Typescript类方法或属性 - Access Typescript class method or attribute from inner function of another method 如何在TypeScript中为变量定义静态类类型? - How to define static class type for a variable in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM