简体   繁体   English

TypeScript中的泛型

[英]Generics in TypeScript

Is there a way to parameterize a type with another type in TypeScript besides of using typed arrays? 除了使用类型化数组之外,有没有办法在TypeScript中使用其他类型参数化类型?

It is really necessary with KnockoutJs. KnockoutJs真的很有必要。

Generics are not supported as yet, though they are being considered. 虽然正在考虑泛型,但尚未支持泛型。 Here's what the spec has to say: 这是规范必须说的:

NOTE: TypeScript currently doesn't support Generics, but we expect to include them in the final language. 注意:TypeScript目前不支持Generics,但我们希望将它们包含在最终语言中。 Since TypeScript's static type system has no run-time manifestation, Generics will be based on “type erasure” and intended purely as a conduit for expressing parametric type relationships in interfaces, classes, and function signatures. 由于TypeScript的静态类型系统没有运行时表现形式,因此泛型将基于“类型擦除”,并且纯粹用作表达接口,类和函数签名中的参数类型关系的管道。

From the TypeScript language spec at the end of section 3. 来自第3节末尾的TypeScript语言规范。

Generics are finally here: http://blogs.msdn.com/b/typescript/archive/2013/06/18/announcing-typescript-0-9.aspx 泛型终于来了: http//blogs.msdn.com/b/typescript/archive/2013/06/18/announcing-typescript-0-9.aspx

As of now it is in beta, so use it with caution. 截至目前,它处于测试阶段,因此请谨慎使用。

I'm using a rather dirty workaround. 我正在使用一个相当肮脏的解决方法。 It's possible to assign a class to a variable of type any. 可以将类分配给any类型的变量。 This code is valid: 此代码有效:

class A{}
var test:any=A;
var a=new test();

So you can parametrize your methods by adding another parameter of type any 因此,您可以通过添加另一个类型为any的参数来参数化您的方法

function(param:any){
    var test=new param();
    test.someFunction();
}

Of course this is very bad style and probably not recommended. 当然这是非常糟糕的风格,可能不推荐。 But for me it will cover the time till generics are included in the language. 但对我而言,这将涵盖语言中包含泛型的时间。

For those like me who come across this question now that we have Generics in TypeScript here is a little more info including a link to the official documentation on Generics on the Typescript website as that explains it well, will hopefully always stay up-to-date as changes are made, and shows example usage: 对于像我这样现在遇到这个问题的人,我们在TypeScript中有Generics,这里有一些信息,包括在Typescript网站上关于Generics的官方文档的链接,因为它解释得很好,希望始终保持最新进行更改时,显示示例用法:

https://www.typescriptlang.org/docs/handbook/generics.html https://www.typescriptlang.org/docs/handbook/generics.html

Generics allow the creation of components that can work over a variety of types rather than a single one. 泛型允许创建可以在各种类型而不是单个类型上工作的组件。

As shown in the official docs the identity function is the most basic illustration of Generics at work. 正如官方文档中所示,身份功能是泛型工作中最基本的例证。 The identity function is a function that will return back whatever is passed in. 标识函数是一个函数,它将返回传入的内容。

Here's what our options would have been before Generics: 以下是我们在Generics之前的选择:

// without Generics option 1 - explicitly define and get tied to a single type.  
function identity(arg: number): number {
    return arg;
}

// without Generics option 2 - use the 'any' type 
// but lose type information on the incoming arg by the time we return it.
function identity(arg: any): any {
    return arg;
}

And here's how it works with Generics: 以下是Generics的工作原理:

// with Generics - use a type variable T that works on types rather than values. 
// Captures the type of incoming arg so we can use it again in the return type
function identity<T>(arg: T): T {
    return arg;
}

// can call it with explicit setting of T to be a given type ('string' here)
let output = identity<string>("myString");  // type of output will be 'string'

// However can also call it without this explicit typing and the compiler will 
// infer the type. Note this won't always work for more complex Generics usage
let output = identity("myString");  // type of output will be 'string'

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM