Gabriel Machado
Gabriel Machado

Reputation: 493

what means a type like SomeType<T, U, V> in typescript?

I'm using d3 with typescript and there is a lot of types in d3 that are like this SomeType<U,T,V>. Example:

merge(other: Selection<GElement, Datum, PElement, PDatum>): Selection<GElement, Datum, PElement, PDatum>

I looked through the advanced types documentation but could not understand what these types mean. I can't say if they are a Selection type with these subtypes or whatever.

Upvotes: 4

Views: 2345

Answers (2)

Alex Wayne
Alex Wayne

Reputation: 187184

Those are generics. And put simply they let you parameterize a type, allowing you pass other types into it.

So to use your example, you could do something like:

interface SomeType<T, U, V> {
  t: T
  u: U
  v: V
}

const foo: SomeType<string, number, { cool: boolean }> = {
  //                T       U       V
  t: 'a string',
  u: 123,
  v: { cool: true }
}

Playground

Lots of documentation on generics here: https://www.typescriptlang.org/docs/handbook/generics.html

Upvotes: 6

Jose Pablo Alvarado
Jose Pablo Alvarado

Reputation: 33

I am not sure on typescript, but at least on some other languages such as C# the T type is a Generic Type Parameter basically it means that you dont need to specify a concrete type of object.

Upvotes: 2

Related Questions