Why do we use "<>" in Typescript?

Asked

Viewed 133 times

6

Why do we use <> in Typescript? Example:

funcaoExemplo(x: X<any>, x: X): Observable<X<any>>

1 answer

9


This is to indicate a type parameterization, also known as Generics (some like to call Diamond Operator but he’s not an operator. It works like the parentheses of a function, in fact it is very similar since the parentheses are used to contain the parameters (or arguments, depends on where you’re using, at some point the <> is used as a generic argument as well). What’s inside is just the kind you can use, in this case any means you can use anyone, in this case it’s a generic argument since you’re consuming the type x which is generic.

He kind of remembers the idea of array in static typing languages, know when to use:

int[]

then, speaking abstractly, just to understand, it doesn’t work like that, generically it would be something like this:

Array<int>

where it reads "a array whole".

I put in the Github for future reference.

In statically typed languages you have to say the type for everything, including what goes inside an object. There is no effectively static language without Generics, has a patch, but breaks the typing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.