简体   繁体   English

TypeScript 中的通用构造函数引用和静态方法

[英]Generic constructor reference and static method in TypeScript

Please, consider the following code:请考虑以下代码:

class Wrapper<T> {

    public static forConstructor<S extends Object>(construc: { new (...args: any[]): S }): Wrapper<S> {
        return new Wrapper<S>();
    }
}

class A {
    private aaa: string = null;
}

class B {
    private bbb: string = null;
}

const wrapper: Wrapper<A> = Wrapper.forConstructor(B);// LINE X

At LINE X Wrapper<A> = Wrapper<B> that is an wrong, however, TypeScript doesn't show error at this line.在 LINE X Wrapper<A> = Wrapper<B>这是错误的,但是,TypeScript 不会在此行显示错误。 What is my mistake?我的错误是什么?

Currently Wrapper<A> and Wrapper<B> are structurally compatible.目前Wrapper<A>Wrapper<B>在结构上是兼容的。 If you'll store the passed constructor as a field (for example) you'll get an error:如果您将传递的构造函数存储为一个字段(例如),您将收到一个错误:

type Constructor<T> = new (...args: any[]) => T;

class Wrapper<T> {
    constructor(private c: Constructor<T>){}

    public static forConstructor<T>(construc: Constructor<T>): Wrapper<T> {
        return new Wrapper<T>(construc);
    }
}

class A {
    private aaa: string = null;
}

class B {
    private bbb: string = null;
}

const wrapper: Wrapper<A> = Wrapper.forConstructor(B); // error

Playground 操场

A static method can not use the instance type argument Wrapper<T> since static is not instance bounded.静态方法不能使用实例类型参数Wrapper<T>因为静态方法不受实例限制。 Your method signature <S extends Object essentially means any Object.您的方法签名<S extends Object本质上意味着any Object。 So there is no type safety at all.所以根本就没有类型安全。 That's why the tscompiler does not complain at这就是为什么 tscompiler 不会抱怨

const wrapper: Wrapper<A> = Wrapper.forConstructor(B);// LINE X

However if you actually use the instance type argument and make it non-static then it will complain eg但是,如果您实际使用实例类型参数并将其设为非静态,那么它会抱怨,例如

class Wrapper<S> {

    public forConstructor(construc: { new (...args: any[]): S }): Wrapper<S> {
        return new Wrapper<S>();
    }
}

const wrapper: Wrapper<A> = new Wrapper();
wrapper.forConstructor(B); // will not work

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

相关问题 Typescript 通用 Class 构造函数参数与 static 方法 - Typescript Generic Class Constructor Argument with static methods 关于 TypeScript 中泛型构造函数引用的问题 - Issue about generic constructor reference in TypeScript 将 static 方法引用传递给 TypeScript 中的超级构造函数时避免“未绑定函数”ESLint 警告 - Avoid 'unbound function' ESLint warning when passing a static method reference to a super constructor in TypeScript 在 Typescript 中有静态创建方法而不是构造函数吗? - Have a static create method instead of a constructor in Typescript? Typescript调用泛型类型的静态方法 - Typescript call static method of generic type TypeScript:如何访问泛型类的静态方法 - TypeScript: how to access a static method of a generic class 打字稿类型中的引用静态方法:自动获取方法签名 - Reference static method in typescript type: Automatically pick up method signature 静态构造函数打字稿 - Static constructor typescript 在 TypeScript class 上添加抽象/可继承 static 构造函数辅助方法 - Adding abstract/inheritable static constructor helper method on TypeScript class 如何在TypeScript中使用静态方法和构造函数签名声明接口? - How to declare interface with static method and constructor signature in TypeScript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM