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?
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 .
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.
Angle brackets are commonly used to enclose a code of some type. For example, HTML tags and PageMaker tags are enclosed in angle brackets.
Let's break it down, shall we?
function myFn<T>(param: T): T {
return param;
}
function
: its Typescript keyword, denoting that you are declaring a function.myFn
: the name of the function
in point 1.<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.(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.:T
: This denotes that the function which you declared in point 1 will have to return a value of type T
.{ 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
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.
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