Data type comparison

Js raw data type Ts New data type
number Tuple (tuple)
string Enum (enum)
boolean Any (any type)
null Void (no type)
undefined Never (class of values that never exist)
object
symbol

The data type

  • Boolean value Boolean
  • Digital number
  • String string
  • An array of array
  • Tuples tuple
  • Enumeration enum
  • Any arbitrary value
  • There is no type void
  • Null, and undefined
  • The value never never exists
  • Non-primitive type Object

Boolean values, numbers, strings

Relatively simple

/ / a Boolean value
let b: boolean = true;

/ / digital
let n: number = 10;

/ / string
let s: string = "ts";
Copy the code

An array of

There are two ways to define an array:

Type 1: element type followed by []

let list: number[] = [1.2.3];
let list1: string[] = ["a"."b"."c"];
Copy the code

Array< element type >

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

tuples

Represents an array with a known number and type of elements, not necessarily the same type.

let t: [string, number] = ["t".1];
// Union types are used when accessing out-of-bounds elements
// that is, as long as the subscription type can be used
t.push("s");
t.push("s");

// There will be an error
t[100] = 2; // Cannot assign type "2" to type "undefined". Tuple type "[string, number]" of length "2" has no elements at index "100".
Copy the code

The enumeration

Enumeration types are used to give friendly names to a set of values.

/ / the enumeration
enum Color {
  Red,
  Green,
  Blue,
}

let c: Color = Color.Red;
Copy the code

By default, elements are numbered from 0

You can also specify the value of the member manually, and then increment the value of the unspecified member

enum Color {
  Red = 1,
  Green,
  Blue,
}

let c: Color = Color.Red;
Copy the code

Alternatively, all manual assignments are used:

enum Color {
  Red = 1,
  Green = 3,
  Blue = 5,}let c: Color = Color.Red;
Copy the code

We can also find the corresponding name based on the enumerated value

let cName: string = Color[1];
Copy the code

any

The type of the variable is uncertain

let a: any = 1;
a = "ts";
Copy the code

void

In contrast to any, it means that there is no type. It is usually used when a function returns no value

function v() :void {
  console.log("void");
}
Copy the code

Null, and undefined

Their various types are themselves, and of little use.

never

A value type that never exists. Usually a type that throws an exception or never returns a value

function nev() :never {
  throw "123";
}

function infiniteLoop() :never {
  while (true) {}}Copy the code

object

Object represents a non-primitive type, that is, a type other than number, string, Boolean, symbol, NULL, or undefined

let o: object;
o = [1.2.3];
// o = 1; // Type "number" cannot be assigned to type "object".
Copy the code

Types of assertions

Like type conversion. We know the details of a value better than the compiler and skip validation with type assertions

let s: any = "this is ts";

let len: number = s.length;
Copy the code

The s defined above is of type any, but the actual assignment is a string. It’s possible to get an error if you value it directly, but we know for sure that it’s a string, so we can use type assertions

Type assertions come in two forms. The first is the Angle bracket syntax:

let len: number = (<string>s).length;
Copy the code

The other is the as syntax:

let len: number = (s as string).length;
Copy the code