TypeScript 中的泛型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12886916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:15:51  来源:igfitidea点击:

Generics in TypeScript

genericstypescript

提问by Trident D'Gao

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 真的很有必要。

采纳答案by Trident D'Gao

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/annoucing-typescript-0-9.aspx

As of now it is in beta, so use it with caution.

目前它处于测试阶段,因此请谨慎使用。

回答by Brian Terlson

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. 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 目前不支持泛型,但我们希望将它们包含在最终语言中。由于 TypeScript 的静态类型系统没有运行时表现形式,泛型将基于“类型擦除”,并且纯粹是作为在接口、类和函数签名中表达参数类型关系的管道。

From the TypeScript language spec at the end of section 3.

来自第 3 节末尾的 TypeScript 语言规范。

回答by TornadoAli

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 中有泛型,这里有更多信息,包括指向 Typescript 网站上泛型官方文档的链接,因为它解释得很好,希望始终保持最新状态随着更改的进行,并显示示例用法:

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.

如官方文档所示,identity 函数是泛型在工作中最基本的说明。身份函数是一个函数,它会返回传入的任何内容。

Here's what our options would have been before 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:

这是它如何与泛型一起工作:

// 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'

回答by lhk

I'm using a rather dirty workaround. It's possible to assign a class to a variable of type any. This code is valid:

我正在使用一种相当肮脏的解决方法。可以将一个类分配给 any 类型的变量。此代码有效:

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.

当然,这是非常糟糕的风格,可能不推荐。但对我来说,它将涵盖泛型包含在语言中的时间。