This is my 24th day of the August Update Challenge. There are already some data types in JavaScript that don’t work well with TypeScript, so TypeScript introduces new types to enhance TypeScript capabilities

Let’s take a look at TypeScript data types

The syntax for specifying a type for a variable is to use the form “variable: type” as follows:

let num: number = 123
Copy the code

If you do not specify a type for the variable, the compiler will automatically infer the type of the variable based on the value you assign to it:

let num = 123
num = 'abc' // Error cannot assign type "123" to type "number"
Copy the code

When we assign num to 123 but do not specify a type, the compiler concludes that num is of type number, so an error is reported when we assign num to the string “ABC”.

Note the difference between number and number: TS uses number when specifying a type, which is the TypeScript type keyword. Number, which is JavaScript’s native constructor, is used to create values of numeric type. String, Boolean, etc., which you’ll see later, are TypeScript type keywords, not JavaScript syntax.

Number

In TypeScript, like JavaScript, all numbers are floating point numbers, so there is only a number type, not an int or float. TypeScript also supports binary and octal numeric literals new to ES6, so TypeScript supports two, eight, ten, and sixteen digits.

let num: number = 123
Copy the code

Once the data type is initialized, it cannot be assigned to any other data type

num = '123' // Error cannot assign type "123" to type "number"
num = 123 / / right
Copy the code

Boolean

let bol: boolean = true;
Copy the code

String

let str: string = 'str';
Copy the code

Array

let list1: number[] = [1.2.3];
let list2: (number | string) [] = [1.2.'nordon'];
let list3: Array<string> = ['str']; // Not recommended
Copy the code

The first form specifies that the elements of this type are arrays of type number, using the form number[]. This is recommended, but you can use the second. Note that number specifies the type of the array element, and you can specify any type of array element here.

Null&Undefined

Null and undefined have some characteristics in common, so they are used together.

Common features: This is because undefined and NULL are two basic data types in JavaScript.

let unde: undefined = undefined;
let n: null = null;
Copy the code

In TypeScript, both of these have their own types undefined and null, which means they are both actual values and types. By default, undefined and NULL can be assigned to any type of value, This means you can set undefined to void or number when you set strictNullChecks to the “compilerOptions” of tsconfig.json: True, that must be treated strictly. Undefined and null can only be assigned to themselves and void types,

tuple

A tuple can be thought of as an extension of an array, representing the number and type of known elements, i.e. the known length of the array and the data type at each location

When assigning a value to a tuple, the number and type must match one by one

let tuple1: [number.string.boolean] = [1.'nordon'.true];

// tuple1[1] = 1; Error, it should be string
tuple1[1] = 'wy';
Copy the code

enum

TypeScript adds enumerated types to ES, and in TypeScript you can name a set of values, which is more developer-friendly. For example, if we want to define a set of roles, each of which is represented by a number, we can define it using an enumerated type

Will default to each allocation number of enum, starting at 0 by default

enum Roles1 { // {0: "SUPER_ADMIN", 1: "ADMIN", 2: "USER", SUPER_ADMIN: 0, ADMIN: 1, USER: 2}
  SUPER_ADMIN,
  ADMIN,
  USER
}
Copy the code

any

If you’re writing code and you don’t know exactly what type the data is, you can use any. Better advice is to use unknown

Any needs to be used with caution; if you use any on a large scale, TypeScript makes little sense

let anyArr: any[] = [1.true];
let any1: any;
any1 = 1;
any1 = 'any str';
Copy the code

void

Is there any type? Is there any type

Often used to indicate that a function has no return value

Void variables can only be assigned to null and undefined. Other types cannot be assigned to void variables

function voidFun() :void {
  console.log("VoidFun doesn't return anything, just log it.");
}
Copy the code

never

The type of value that never exists. It is the return type of a function expression that always throws an exception or returns no value at all. A variable bound by type protection that never is true is also of type never

An exception is always thrown, so the return value is of type never, indicating that its return value never exists

function neverErr() :never {
  throw new Error('throw exception never');
}
Copy the code

unknown

It is an unknown type. The difference between unknown and any is that unknown is safer than any

Why is Unknown more secure than any?

If a variable is set to type any, the variable is invalidated, and the js variable can be accessed by any attributes or methods

If a variable is of type unknown, it cannot be operated on without being narrowed down by type assertion based on control flow

symbol

A new data type in Es6

let symbol1: symbol = Symbol('s');
Copy the code