TypeScript is a superset of JavaScript. It is used to highlight unexpected behavior in your code, lowering the chance of bugs.
Installing TypeScript:
$ npm install -g typescript
on your terminal
check version: tsc -v
configure file and keep compiler on, type $ tsc-w in terminal
One way TypeScript does this is for you to tell what the types should be. TypeScript will throw an error if you use a different type.
For functions you can declare type on the parameters and type you want to return
Interfaces- helps us describe entities
? makes key not mandatory and should be placed before the colon
Union Operators- | stands for or
Most popular use for union is to check for null
Alias- can replace type with aliases (should be capitalized)
Combining Union Operators and Aliases
Void data type- set of undefined and null (also used if we don't want a return value)
Any type- Can be any type (not very useful, do not use, turns off checks)
Never Type- function with never cant be executed to the end (function) that will never end
Unknown type when you don't know what type you want it to be. You can fix the error below by changing pageNumber: from string to unknown
Type assertion - "as" keyword is known as Type assertion
TypeScript working with DOM - TypeScript has a lot of types for DOM already built in
Next Blog: Classes, generic interfaces, enums, and Typescript in React
Reference:
TypeScript official documentation:
https://www.typescriptlang.org/docs/handbook/intro.html
https://www.youtube.com/watch?v=gp5H0Vw39yw&ab_channel=freeCodeCamp.org
Top comments (0)