Classification of base types

classification

  • Boolean: Indicates a Boolean value
  • Number: supports 2/8/10/16
  • String: string
  • Array: a common array, which can be either string[] or array
  • Tuple: a special array that specifies the type of each element in the array and the number of elements
  • Any: variable of uncertain type
  • Enum: Enumeration type. Key can be found based on value
  • Void: a function that does not return any value
  • Undefined /null: two special types
  • <> : Very definite type

What’s the difference between arrays and tuples?

Array: Merges the same type

Tuples: Combine objects of different types, commonly used in functional programming languages

Void and undefined/null?

  • Undefined and NULL are subtypes of all types.
  • Undefined can be assigned to a variable of type number
  • Void cannot be assigned to a variable of type number
// This will not report an error
let num: number = undefined;
// This will cause an error
let u: void;
let num: number = u;
// Type 'void' is not assignable to type 'number'.
Copy the code

boolean

// boolean
let isDone: boolean = true;
let disabled: boolean = false;
console.log(`isDone: ${isDone}`);   // isDone: true
console.log(`disabled: ${disabled}`); //disabled: false
Copy the code

number

// number 
// decimal, hexadecimal, octal, binary
let n1: number = Awesome!;
let n2: number = 0x0a;
let n3: number = 0o17;
let n4: number = 0b11;
console.log(`n1: ${n1}`); // n1: 666
console.log(`n2: ${n2}`); // n2: 10
console.log(`n3: ${n3}`); // n3: 15
console.log(`n4: ${n4}`); // n4: 3
Copy the code

string

// string
// Support template strings
let myname: string = 'john';
let myage: number = 18;
let greeting: string = `i am ${myname} and i am ${myage}years old! `
console.log(greeting); // i am john and i am 18 years old!
Copy the code

array

Method 2: Array is Array Generic

// Two array definitions
let arr1: string[] = ['a'.'b'];
let arr2: Array<number> = [1.2.3];

console.log(arr1); // [ 'a', 'b' ]
console.log(arr2); // [1, 2, 3]
Copy the code

What is a class array?

For example, arguments is an array of classes and cannot be received with a normal array,

Interface should be used to receive messages such as IArguments, NodeList, HTMLCollection, etc

For example let args:IArguments = arguments

tuple

/ / the tuple tuples
// Special arrays define arrays of types and numbers (specify the type of each element)
let tp1: [number.string] = [1.'abc'];
let tp2: [number.string.string] = [1.'abc'.' '];

tp2[2] = 'x';
// tp2[3] = 'y'; // After a tuple is defined, no new elements can be added

console.log(tp1); // [ 1, 'abc' ]
console.log(tp2); // [ 1, 'abc', 'x' ]
Copy the code

After a tuple is defined, no new elements can be added.

Y is not assignable to type

enum

Usage: First define enumeration set, delimit constant range

Enumeration function: You can obtain the corresponding key by subscripting

// enum
// You can assign all values by default, some manually, and all manually
enum Color {Red = 2, Green = 1, Blue};

let c1: Color = Color.Red; // c1=2
let c2: Color = Color.Green;  // c2=1 
let c3: Color = Color.Blue; // c3=2

let cname1: string = Color[2];
console.log(cname1); // 'Blue' corresponds to key
Copy the code

any

Arbitrary value: variable of indeterminate type

The variable declaration does not specify its type

  • Can be recognized as any value type
  • let a; Equivalent to let a:any;

Usage Scenarios:

  1. You don’t want the type checker to check these values but to pass them directly through compile-time checks.
  2. A compile-time error is reported by running a call to a nonexistent method
// any 
let notSure: any;

notSure = 1;
console.log(notSure); / / 1

notSure = 'abc';
console.log(notSure); // 'abc'
Copy the code

void

Void means there is no type. Usage scenario: When a function returns no value

// void
function f1() :void{
    console.log('no return');
}
function f2() :string{
    return 'have return'
}
console.log(f1()); // void -> no return
console.log(f2()); // have return
Copy the code

undefined null

// undefined null
let empty1: undefined = undefined;
let empty2: null = null;

console.log(empty1); // undefined
console.log(empty2); // null
Copy the code

Built-in objects

Ts.xcatliu.com/basics/buil…

  • ECMAScript built-in objects Boolean, Error, Date, RegExp
  • The built-in objects of DOM and BOM are Document, HTMLElement, Event and NodeList
let body: HTMLElement = document.body;
let allDiv: NodeList = document.querySelectorAll('div');
document.addEventListener('click'.function(e: MouseEvent){
    // do something
})
Copy the code

Type inference

Ts automatic type inference (similar to automatic type conversion)

  1. 4. To infer [type] from [assignment].
  2. No assignment when defined: inferred to [type any]
let  myName = 'Jim'; // The inference type is string
myName = 7; //error: type number 
Copy the code
let  myName; // The inferred type is any
myName = 'Jim'; //ok
myName = 7; //ok
Copy the code

The joint type

Simple example: use | separated for each type (it may be a variety of types)

Access properties and methods: Only public properties or methods can be accessed

Type inference: If there is assignment, there is type inference

Type the alias

Syntax: The keyword is type

Purpose: Commonly used for union types

Types of assertions

Type assertion: Indicates that the programmer is sure of the type and can perform automatic type conversion

Grammar:

  • < type > values, such as STR, counter

  • Value as type, such as STR as string, counter as number

// <>
// Type assertion,
let verysure: any = 'very sure type';
let verysureLength: number = (<string>verysure).length;
console.log(verysure); // very sure type
console.log(verysureLength); / / 14
Copy the code

Usage: Definitely one of the union types

Note: Type assertions are not type conversions, and assertions to nonexistent types are not allowed

String Optional value

Purpose: Constraint string type, can only be one of several strings

Syntax: Type and delimiter definitions

The generic

Action: [do not specify] specific type, [specify when used] type

Grammar:

  1. Define generic parameters after the function name,

  2. Function input parameters and return values use generic parameters,

  3. Support for single generics and multigenerics

Note: You do not know what type a generic is beforehand, so you cannot manipulate its properties or methods arbitrarily

Can be compatible with different types, according to the input value to determine the type

A statement to merge

What is?

If you define two functions, interfaces, or classes with the same name, they are merged into one type

Pay attention to

If the attribute has the same name, the type of the merged attribute must be unique

Merge types: function merge, interface merge, class merge