Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Confusion about angle brackets in TypeScript [duplicate]

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?

like image 542
SeBe Avatar asked Jan 19 '18 09:01

SeBe


People also ask

What do the angled brackets mean in TypeScript?

One within the parentheses () and another within the angle brackets < > We know in the function call func(12) , argument 12 within the parentheses represents the arg parameter. Similarly in func<number>(12) , the argument number within the angle brackets represents the generic type parameter T .

What is in TypeScript <>?

TypeScript is a syntactic superset of JavaScript which adds static typing. This basically means that TypeScript adds syntax on top of JavaScript, allowing developers to add types. TypeScript being a "Syntactic Superset" means that it shares the same base syntax as JavaScript, but adds something to it.

What are angle brackets used for in JavaScript?

Angle brackets are commonly used to enclose a code of some type. For example, HTML tags and PageMaker tags are enclosed in angle brackets.


2 Answers

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

like image 123
CozyAzure Avatar answered Sep 25 '22 15:09

CozyAzure


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.

like image 20
Dan Prince Avatar answered Sep 23 '22 15:09

Dan Prince