Trident D'Gao
Trident D'Gao

Reputation: 19680

Generics in TypeScript

Is there a way to parameterize a type with another type in TypeScript besides of using typed arrays?

It is really necessary with KnockoutJs.

Upvotes: 16

Views: 5758

Answers (4)

TornadoAli
TornadoAli

Reputation: 2195

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:

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:

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

Upvotes: 0

Trident D&#39;Gao
Trident D&#39;Gao

Reputation: 19680

Generics are finally here: 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.

Upvotes: 14

lhk
lhk

Reputation: 30006

I'm using a rather dirty workaround. It's possible to assign a class to a variable of type 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

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.

Upvotes: 0

Brian Terlson
Brian Terlson

Reputation: 9630

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.

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

Upvotes: 18

Related Questions