This is the 24th day of my participation in the August More Text Challenge

TS first

  • Ts files cannot be run by the browser. Compile them into js files using TSC + *. Ts

  • Every time you modify the TS file, you need to recompile it into a JS file, which is very troublesome, so you can configure yourself

    1. TSC –init generates a. Json file
    2. Open the tsconfig.json file to modify and delete the corresponding configuration (if you want to change the configuration quickly, copy the following configuration)
    {
    "compilerOptions": {
     "target": "es5"."noImplicitAny": false."module": "amd"."removeComments": false."sourceMap": false."outDir": "js"// The directory where you want to generate js}}Copy the code
  • Terminal => Running tasks => TS: watch

Why do we need TS

  1. Support for object-oriented programming features such as classes and interfaces
  2. Error checking is provided at compile time. It compiles the code, and if it finds any errors, it highlights them before running the script.
  3. Supports the latest JavaScript features, including ECMAScript 2015

TS data Definition

JavaScript comes in two types: Primitive data types and Object types.

Primitive data types include: Boolean, numeric, string, NULL, undefined, and the new type Symbol in ES6

The original data

Primitive types are all defined

let isDone:boolean = false;
let num:number = 12.6;
let str:string = '444';
Copy the code

let isDone:Boolean = new Boolean(1); Note that objects created using the constructor Boolean are not Booleans:. In fact, using new Boolean() returns a Boolean object. In TypeScript, Boolean is the basic type in JavaScript, and Boolean is the constructor in JavaScript. The other basic types (except null and undefined) are not mentioned here.

An array of

Method one:

  • Let arr: number [] = (4 and 6);

  • To define an array, specify type :\

let arr1:string[] = ['4']

Method two: generics

  • let arr2:Array<string|number|any> = ['4']

The enumeration

For example, 1 indicates success and 0 indicates failed

Names usually start with a capital letter

enum Flag {success=1,failed=0};
var f :Flag = Flag.success; / / 1
Copy the code

In this way, you can use identifiable values to represent a status, so that you don’t forget exactly what represents success and what represents failed, just store it in success and failed. This is the basic use of enumerated types

Enum Color {‘ RED ‘,’blue’} // RED =>0 Blue =>1 If no value is assigned, the default value is index

Any type

Let oBox:any = document.getelementById (‘box’)

The function definitions

A function can define a type for a return value

  • Void is defined when no value is returned
function fun() :void{
    console.log(1);
    
}
Copy the code
  • If there is a return value, define it as the corresponding type
function fun() :number{
    return 123
}
Copy the code

Never type (rarely used)

Other types, that is, subtypes that are not in the above type (including null and undefined), represent values that never appear. Variables that declare never can only be assigned by type never

let f:never;
f = (() = >{
    throw new Error('err')
    
})();
Copy the code