当前位置:网站首页>Typescript raw data type

Typescript raw data type

2022-06-24 15:16:00 Emperor_ LawD

data type

summary

  • The type declaration is TS A very important feature
    • Type declarations allow you to specify TS Medium variable ( Parameters 、 Shape parameter ) The type of
    • After specifying the type , When assigning a value to a variable ,TS The compiler will automatically check whether the value conforms to the type declaration , If yes, the value is assigned , Otherwise, the report will be wrong
    • In short , The type declaration sets the type... For the variable , So that variables can only store certain types of values
  • TypeScript There are two types of data in
    • Raw data type ( Basic data type )
    • object type ( Complex data type )
  • Common basic data types :number / string / boolean / undefined / null
  • Automatic type determination
    • TS Have automatic type judgment mechanism
    • Simultaneous declaration and assignment of variables ,TS The compiler determines the type of variable
    • So if you declare and assign variables at the same time , You can omit the type declaration

type

type

Example

describe

number

1,-2,3.4

Arbitrary number

string

"hi",'hi',hi

Any string

boolean

true、false

Boolean value true or false

Literal

Its own

Limiting the value of a variable is changing the literal value

any

*

Any type

unknown

*

Type safe any

void

Null value (undefined)

No value ( or undefined)

never

No value

Cannot be any value

object

(name: ' ROM. ')

Any of the js object

array

1, 2, 3

Any of the js Array

tuple

4, 5

Tuples ,TS New type , Fixed length array

enum

enum(A, B)

enumeration ,TS New type in

stay ES6 and ES10 New basic data types are introduced in , Namely Symbol and BigInt

Raw data type

Numeric type

Use keywords number Define the number type

let decimal: number = 6.1; //  decimal 

let hex: number = 0xf00d; //  Hexadecimal 

let binary: number = 0b1010; //  Binary system 

let octal: number = 00744; //  octal 

let big: bigint = 100n;

String type

Use keywords string Define string type

let color: string = 'bule';
color = 'red';

let fullName: string = 'Law D';
let age: number = 12;
let sentence: string = `Hello, my name is ${fullName}.I am ${age} years old.`
//  among  ``  Used to define  ES6  Template string in , Its effect is equal to 
let sentence: string = 'Hello, my name is' + fullName + '.I am' + age + 'years old.'

Boolean type

Use keywords boolean Define Boolean types

let isDone: boolean = false;

notes : Non strict mode number、string、boolean All values can be empty

Void type

stay TypeScript in , It can be used void Represents a function that does not have any return value

function hello(): void {
    alert("Hello TS");
}

let unuseable: void = undefined; //  Declared but unassigned variable values ( Value not found )

Null type

Indicates that the object is missing

let nu: null = null; //  Declared and assigned ( Can find , The value is null)

Undefined type

Used to initialize a variable to an undefined value

let un: undefined = undefined;
  • void and undefined The difference between
let un: undefined;
let num: number = un; //  Can be  undefined  A variable of type is assigned to  number  Variable of type 

let vo: void;
let num: number = vo; // void  Variables of type cannot be given to  number  Variable assignment of type 

undefined and null Is a subtype of all types

原网站

版权声明
本文为[Emperor_ LawD]所创,转载请带上原文链接,感谢
https://yzsam.com/2021/05/20210518144221327r.html