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.
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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With