18

I am currently learning TypeScript and running into some confusion for what angle brackets are used.

I know that you can use it e.g. for generic types, then the brackets would stand behind:

function myFn<T>(param: T): T {
  return param;
}

Also if you define a type for a generic type:

let identity = myFn<string>("hello world");

And that for arrays you can use it for one of two ways to define the types in an array:

let myArr: Array<number>;

For what cases would the brackets stand in front of a word? What other use cases are there to use angle brackets?

SeBe
  • 926
  • 3
  • 9
  • 13
  • should the http://www.typescriptlang.org/docs/handbook/generics.html be copied here? :) – smnbbrv Jan 19 '18 at 09:13
  • 1
    Yes I read through the generic parts of that handbook. And apparently that is the part that I understood. But reading through the rest of the documentation there happen to be a lot of examples where it is not really clear to me what the angle brackets are used for. Especially when it comes first and not after. – SeBe Jan 19 '18 at 09:19
  • 7
    @Aniket Saharwat thanks captain obvious. As I told you I AM reading through that handbook, but that doesn't necessarily means that you understand every part of it. – SeBe Jan 19 '18 at 09:23

2 Answers2

62

Let's break it down, shall we?

function myFn<T>(param: T): T {
  return param;
}
  1. function: its Typescript keyword, denoting that you are declaring a function.
  2. myFn: the name of the function in point 1.
  3. <T>: This means that the function declare is gonna use a generic type: either in the arguments that it's gonna take in, or the return type. You, as a developer, will need to set it explicitly.
  4. (param:T): You have exactly one argument, by the name of param. At the same time, param is of type T, which you have set so in point 3.
  5. :T: This denotes that the function which you declared in point 1 will have to return a value of type T.
  6. { return param }: returns the value of param. Note that this is allowed, because param is of type T and your function needs to return a type T. Let's consider another snippet:

This is not allowed:

function myFn<T>(param: T): T {
  return 0;
}

Reason being 0 is of type number and not type T.

Let's put it in pure English:

You are declaring a function called myFn which gonna take in an argument of type T and is going to return a value of type T.

That's it. Now, on the real usage:

let identity = myFn<string>("hello world");

The variable identity will be reference of a function that takes in an argument(remember params?) which is of type string. It will return the value of type string as well -- and according to the function declaration, it is going to return "hello world".

You can read more on Type Assertion of Typsecript

CozyAzure
  • 8,280
  • 7
  • 34
  • 52
13

It's an alternative syntax for type assertion:

let a = 1 as any;
// is equivalent to
let a = <any>1;

That's useful when you want the compiler to treat a value as specific type that's different to the one it has already been assigned or inferred for it.

Dan Prince
  • 29,491
  • 13
  • 89
  • 120