This is the 7th day of my participation in the August Text Challenge.More challenges in August

What is TypeScript?

TS is a superset of JavaScript types that can be compiled to pure JavaScript. It runs on any browser, any computer, and any operating system, and is open source.


How do I use TS?

First, install TS. There are two ways to install TS:

  • Via NPM (Node.js package manager)
  • Install the TypeScript plug-in for Visual Studio

Vscode automatically compiles ts->js

  • TSC –init creates tsconfig.json
  • Modify set js folder :” outDir”:”./js/”
  • Set up the vscode monitoring task, then modify the ts in the project to automatically generate js

We’re going to focus today on the types of TS

What are the types of TS?

TS supports almost the same data types as JavaScript, plus useful enumeration types that we can use in development.


1. Number type number

In addition to supporting decimal and hexadecimal, TypeScript also supports binary and octal.


let a: number = 999;
let b: number = 0o744;
let c: number = 0b1010;
let d: number = 0xf00d;

Copy the code

2. Boolean type Boolean

The value can be true or false


let e: boolean = true;
let f: boolean = false;

Copy the code

3. String String

We use string to represent the text data type. Like JavaScript, you can use double quotes (“) or single quotes (‘) to represent strings.


let name: string = "zhangsan";
name = "lisi";
Copy the code

You can also use template strings in strings


let name: string = ` zhang SAN `;
let age: number = 15;
let story: string = `Hi, my name is ${ name },

I'll be ${ age + 1 } years old today.`;

Copy the code

4. An Array of Array

TypeScript manipulates array elements just like JavaScript. There are two ways to define an array. First, an element type can be followed by [] to represent an array of elements of that type:

let g: number[] = [1.2.3.4.5];
Copy the code

The second way is to use Array generics, Array< element type > :

let list: Array<number> = [1.2.3];
Copy the code

5. Enumeration enum

Enum types complement the JavaScript standard data types. As in other languages, you can use enumerated types to give friendly names to a set of values.


enum Color {Red, Green, Blue}
let c: Color = Color.Green;

Copy the code

6.Any any

Sometimes we want to specify a type for variables whose type is not known at programming time. These values may come from dynamic content, such as user input or third-party code libraries. In this case, we don’t want the type checker to check these values and just pass them through compile-time checks. Then we can mark these variables with type any


let h: any = 7;
notSure = " I have a samll yellow pencil ";
notSure = false;
Copy the code