在Typescript中有“类”类型吗? “任何”包含它吗?

时间:2016-09-08 13:54:23

标签: angular typescript

在Java中,您可以使用“Class”类型将class to a method作为参数。我没有在打字稿文档中找到任何类似的东西 - 是否可以将类交给方法?如果是这样,“any”类型是否包含此类类型?

背景:我遇到了Webstorm的问题,告诉我我无法将一个类移交给Angular 2中的@ViewChild(...)。但是,Typescript编译器并没有抱怨。 @ViewChild()的签名似乎是"Type<any> | Function | string",所以我想知道是否有任何包括类。

9 个答案:

答案 0 :(得分:22)

你在打字稿中提出的等价物是类型{ new(): Class },例如:

class A {}

function create(ctor: { new(): A }): A {
    return new ctor();
}

let a = create(A); // a is instanceof A

code in playground

答案 1 :(得分:6)

  

是否可以将类交给方法?如果是这样,那么类型&#34;任何&#34;包括这样的类型?

是的,是的。 any包括所有类型。

以下是仅包含类的类型示例:

type Class = { new(...args: any[]): any; };

然后使用它:

function myFunction(myClassParam: Class) {
}

class MyClass {}

myFunction(MyClass); // ok
myFunction({}); // error

你不应该在Function的类中传递错误,因为它应该可以正常工作:

var func: Function = MyClass; // ok

答案 2 :(得分:6)

内部declare Type的角为:

export interface Type<T> extends Function { new (...args: any[]): T; }

使用TypeScript3,应该可以添加types for arguments而不会导致函数重载:

export interface TypeWithArgs<T, A extends any[]> extends Function { new(...args: A): T; } 

示例:

class A {}

function create(ctor: Type<A>): A {
    return new ctor();
}

let a = create(A);

答案 3 :(得分:1)

这应该有效 - delcare type 类型

// just two different classes
class MyClass {}
class OtherClass {
    constructor(protected IsVisible: boolean) {}
}

// here we declare our type named "Type"
type Type = Function;

// we will consume just params of a Type (classes)
function take(type: Type){  
}


// build will fail
take(1);          // not a Type
take("A")         // not a Type
take(new Date()); // not a Type

// will be working
take(MyClass);    // this is a type
take(OtherClass); // this is a type

a working example

或类似于界面

// just two different classes
class MyClass {}
class OtherClass {
    constructor(protected IsVisible: boolean) {}
}

// here we declare our type named "Type"
interface Type extends Function {}

// we will consume just params of a Type (classes)
function take(type: Type){  
}


// build will fail
take(1);          // not a Type
take("A")         // not a Type
take(new Date()); // not a Type

// will be working
take(MyClass);    // this is a type
take(OtherClass); // this is a type

示例here

答案 4 :(得分:0)

来自Type<T>

@angular/core 是Class的正确接口。

export interface Type<T> extends Function {
    new (...args: any[]): T;
}

您可以使用它来保留对类的引用,而不是此类的实例:

private classRef: Type<MyCustomClass>;

private classRef: Type<any>;

根据您对@ViewChild提问的背景:

@ViewChild 允许注入"string selector" / Component / Directive

Type<any> | Function | string的签名 是一个抽象签名,可让我们注入以上所有内容。

答案 5 :(得分:0)

最简单的解决方案是let variable: typeof Class

这里有个例子:

class A {
  public static attribute = "ABC";
}

function f(Param: typeof A) {
  Param.attribute;
  new Param();
}


f(A);

答案 6 :(得分:0)

以下为我工作:

type ClassRef = new (...args: any[]) => any;

我的用例:

 interface InteractionType { [key: string]: ClassRef; }

答案 7 :(得分:0)

这是仅包含类的类型的示例:

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

答案 8 :(得分:-2)

Java和JavaScript中的继承模型是不同的。在Java中,您在类的所有实例之间共享一个Class对象。 JavaScript使用原型继承,而不存在Class对象。

TypeScript和ES6都只使用class关键字作为语法糖,而不更改可执行代码的继承模型。