What is it

Typescript is almost identical to javascript in that it has the same data types and provides more useful types for development on top of javascript

At development time, explicit variables can be typed so that typescript can type check at compile time and show errors when the type does not match the expected result

Two, what

Typescript data types are as follows:

  • Boolean (Boolean type)
  • Number (number type)
  • String (String type)
  • Array (Array type)
  • Tuple (tuple type)
  • Enum (Enumeration type)
  • Any (any type)
  • Null and undefined
  • Void type
  • Never type
  • Object Object type

boolean

Boolean type

let flag:boolean = true; // flag = 123; // Error flag = false; / / rightCopy the code

number

Numeric types. Like javascript, typescript numeric types are floating point numbers that support binary, octal, decimal, and hexadecimal numbers

let num:number = 123; // num = '456'; // Error num = 456; / / rightCopy the code

In base form:

let decLiteral: number = 6; // Decimal let hexLiteral: number = 0xf00d; // let binaryLiteral: number = 0b1010; // let octalLiteral: number = 0o744; / / octalCopy the code

string

A string type that, like JavaScript, can be represented by double quotes (“) or single quotes (‘)

let str:string = 'this is ts';
str = 'test';
Copy the code

As a superset, you can of course wrap the template string, embedding variables with ${}

let name: string = `Gene`;
let age: number = 37;
let sentence: string = `Hello, my name is ${ name }
Copy the code

array

The array type, which is javascript like, is wrapped with [], which can be written either way:

Method 1: Element type followed by []

let arr:string[] = ['12', '23'];
arr = ['45', '56'];
Copy the code

Array< element type > :

let arr:Array<number> = [1, 2];
arr = ['45', '56'];
Copy the code

tuple

A meta-ancestor type that allows you to represent an array with a known number and type of elements that need not be of the same type

let tupleArr:[number, string, boolean];
tupleArr = [12, '34', true]; //ok
typleArr = [12, '34'] // no ok
Copy the code

The type, position, and number of the assignment must be the same as the type, position, and number of the definition (declaration)

enum

Enum types complement JavaScript’s standard data types, and use enumerated types to give friendly names to sets of values

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

any

You can specify a value of any type. You can specify a type for a variable whose type is not known at programming time, and you don’t want the type checker to check these values and pass them directly at compile time

Using any allows you to assign to any type and even call its properties and methods

let num:any = 123;
num = 'str';
num = true;
Copy the code

Example code for defining arrays that store various types of data is as follows:

let arrayList: any[] = [1, false, 'fine'];
arrayList[1] = 100;
Copy the code

Null, and undefined

In JavaScript null means “nothing”, which is a special type with a single value, which means an empty object reference, and undefined means a variable with no value set, okay

By default null and undefined are subtypes of all types, which means you can assign null and undefined to variables of type number

let num:number | undefined; // Use undefined console.log(num); Num = 123; console.log(num); / / rightCopy the code

But ts is configured with strictNullChecks, null and undefined can only be assigned to void and their respective checks

void

Used to identify the type of value returned by a method, indicating that the method has no return value.

function hello(): void {
    alert("Hello Runoob");
}
Copy the code

never

Never is a subtype of other types, including null and undefined. It can be assigned to any type and represents a value that never occurs

But no type is a subtype of never, which means that variables declaring never can only be assigned by never.

The never type is typically used to specify an infinite loop that always throws an exception

let a:never; a = 123; A = (() => {// throw new Error(' Error '); Function error(message: string): never {throw new error(message); }Copy the code

object

Object type, non-primitive type, usually wrapped with {}

Third, summary

Basically the same as javascript, also divided into:

  • Basic types of
  • Reference types

Typescript adds primitive types such as void, any, and emum to the base types

reference

  • www.tslang.cn/docs/handbo…

Thank you