简体   繁体   English

如何键入提示类,而不是类的实例?

[英]How can I type hint a class, rather than an instance of the class?

How can I type hint a class, rather than an instance of the class? 如何键入提示类,而不是类的实例?

I tried this, but I get Cannot use 'new' with an expression whose type lacks a call or construct signature. 我试过这个,但是我得到了Cannot use 'new' with an expression whose type lacks a call or construct signature.

class Foo {
}

class Bar extends Foo {
}

class Baz extends Foo {
}

function test(c: Foo) {
    new c();
}

test(Baz);

Use typeof : 使用typeof

class Foo {
}

class Bar extends Foo {
}

class Baz extends Foo {
}

function test(c: typeof Foo) {
    new c();
}

test(Baz);

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

相关问题 如何键入无类型 class 的实例? - How can I type the instance of an untyped class? 如何指定类型是 TypeScript 中给定 class 类型的实例? - How can I specify that a type is an instance of a given class type in TypeScript? 在具有特定类型而不是联合类型的已区分联合中获取类的实例 - Get instance of a class in a discriminated union with its specific type, rather than the union type 如何让 Angular 组件动画遵循 CSS 类更改,而不是静态样式? - How can I make Angular component animations follow CSS class changes, rather than static styles? 如何键入 class 实例的一些可能状态? - How can i type some possible states of a class instance? TypeScript类型检查类型而不是实例 - TypeScript type checking on type rather than instance 如何在 TypeScript class 方法中有条件地提示 boolean 返回类型? - How to hint boolean return type conditionally in TypeScript class methods? TS/Node.js:获取类实例的绝对路径而不是类本身 - TS/Node.js: Getting the absolute path of the class instance rather than the class itself 使用类装饰器,我可以获得类类型实例的类型吗? - Using class decorators, can I get the type of the class-type instance? 如何创建我的Typescript类的泛型类型的新实例? - How can I create a new instance of the generic type of my Typescript class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM