DEV Community

wshi9124
wshi9124

Posted on • Updated on

Intro to TypeScript

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
Image description

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.

Image description

For functions you can declare type on the parameters and type you want to return

Image description

Creating objects
Image description

Interfaces- helps us describe entities
? makes key not mandatory and should be placed before the colon
Image description

Union Operators- | stands for or
Most popular use for union is to check for null
Image description

Alias- can replace type with aliases (should be capitalized)
Image description

Combining Union Operators and Aliases
Image description

Void data type- set of undefined and null (also used if we don't want a return value)
Image description

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
Image description

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
Image description

Type assertion - "as" keyword is known as Type assertion
Image description

TypeScript working with DOM - TypeScript has a lot of types for DOM already built in
Image description
Image description

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)