Small knowledge, big challenge! This paper is participating in theEssentials for programmers”Creative activities

Install the TypeScript

  • First, you need to go to the official website of Node to install Node.js. After the installation is complete, run the following command to check whether the installation is successful.

     node -V 
    Copy the code
  • Next, we install TypeScript globally using the following command:

     npm install -g typescript     
    Copy the code
  • When the installation is complete, use the following command to verify that TypeScript is installed successfully:

     tsc --version   
    Copy the code
  • Once the installation is complete, we can create a new demo.ts file and use the following command:

     tsc demo.ts
    Copy the code
  • When this is done, a demo.js file is generated and can then be executed using the following command:

     node demo.js
    Copy the code
  • To save operations, we can install TS-Node to simplify the above two steps:

    NPM install -g ts-node You can also run the following command to specify the required ts-node version: NPM install -g [email protected]Copy the code

TSC demo.ts and node demo.js can be replaced by ts-node demo.ts

TypeScript language basics

Variable names, also called identifiers, have some rules for defining them:

Allow letters, numbers, underscores, and the dollar sign “$”

Allows the inclusion of Unicode escape sequences

Only letters, Unicode escape sequences, underscores, and dollar signs ($) are allowed as the first character, no numbers are allowed as the first character.

Identifiers are case sensitive

Reserved words are not allowed as identifiers

Variable declaration:

Common variable declarations: let, var, const

Let the statement

When let declares a variable, we can assign an initial value to the variable. If the variable is not assigned, the default value is undefined.

let num;
console.log(num) //undefined
Copy the code

The var statement

When var declares a variable, we can assign an initial value to the variable. If the variable is not assigned, the default value is undefined.

let title;
console.log(title) //undefined
Copy the code

Const statement

Unlike var and let, a const declaration is used to define a constant. It must define an initial value and cannot be reassigned after being declared.

Let and const are declared to form a block-level scope. Declared variables can only be accessed inside the scope, not outside it. Variables declared by var do not have block-level scope.