Home > Web Front-end > JS Tutorial > Learn Typescript with me (Part 1)

Learn Typescript with me (Part 1)

Susan Sarandon
Release: 2025-01-01 08:22:10
Original
917 people have browsed it

Lets recap basics of typescript, I know most of you already know the basics.

Basic types
string
number
boolean
any

These are basic types you will be seeing everywhere, here is example

const name: string = 'Danish' //  a string

let a: number = 23

let b: string = 'somestring'

let e: boolean = false
Copy after login

Learn Typescript with me (Part 1)

Learn Typescript with me (Part 1)

Now when you use these variables typescript will assist you in using them, see if I multiply or divide a with b it will complain, or even when I try to assign a = "Danish" it will complain

Next comes Union types

*Union types * are the mixing of the basic types like string and number, or boolean and number, basically a variable can be both a number or string.

let employeId: string | number
let isSuccess: number | boolean
Copy after login

Learn Typescript with me (Part 1)

here we are assuming that employeId can be string or number, but if we assign boolean to it, typescript will complain

Infer Types, Now typescript infer types based on the value you assigned to the variable. If the value given initially to variable is false let isLoading = false now the infer type of isLoading will be boolean so typescript will complain if we try to do something like isLoading = "ok" .

Learn Typescript with me (Part 1)

Likewise if you don't know the type of some third party package you can just hover over it in your IDE and it will tell you the type.

So this was the basics recap, in next article we will see object, tuple array, optional properties, narrowing and enum.

Danish Ali.

The above is the detailed content of Learn Typescript with me (Part 1). For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template