TypeScript - 2, Types

1. Introduce the concept of type

In JS, variables can be assigned any type.

TS: Variables of this type can only store values ​​of this type.
Report an error if you don't agree.

Example:

let msg:string=123
console.log(msg)

Effect:

insert image description here
js is also generated by default, and it can be configured later so that it will not be generated.

2. Declare the type

a) Basic writing

let name: type=value

let msg:string="你好,世界!"

b) Type inference

The type can be omitted if assignment is made when declaring.
The compiler will automatically add the corresponding type.

let msg="你好,世界!"

//msg也是string类型

3. Five basic types

Types of meaning
number number
string string
boolean boolean
null null
undefined undefined

There's not much to say about that.

4. Miscellaneous types

Types of meaning
any Unlimited type
unknown Unlimited type, cannot be assigned to others
void no value
never no value
basic value only the base value
union type Can store multiple types of values

any

Not limited, when JS is used.

let msg:any
//想放啥放啥

No type declaration, no assignment, will be any.

let msg;
//msg还是any

Not recommended for use.

unknown

any: can be assigned to any variable.

let msg:any
let result:string=msg
//正常运行

unknown: cannot be assigned to others.

let msg:unknown
let result:string=msg
//会报错

How to assign it to others?

  1. Type judgment
let msg:unknown
if(typeof msg==="string"){
    
    
	let result:string=msg
}
//正常运行
  1. type assertion

Tell the parser that this is an xxx type.

let msg:unknown
let result:string=msg as string

Writing two:

let msg:unknown
let result:string=<string>msg

It just doesn't report an error, but it is up to us to judge whether it is of this type or not.

void

A flag indicating that there is no return value.

function a():void {
    
    
	console.log(1)
}

never

A flag to never return.

function abc(): never {
    
    
	throw new Error("error")
}

basic value

A type can be a primitive value, meaning that only this value can be assigned.

let msg:1
//msg只能存放1

union type

Or, can store string, one of boolean

let msg:string|boolean

and, both a name and an age

let msg:{
    
    name:string}&{
    
    age:number}

custom type.

For example, there are only 1234 four values.

type myNum=1|2|3|4;

let msg:myNum

5. Object Type

Types of meaning
object,{ } object
(xxx,xxx)=>xxx function
array array
tuple tuple
enum enumerate

base object

It's just an object, there's no requirement.

let abc:object
let bcd:{
    
    }

Qualified properties.

let abc:{
    
    name:string}

//对象,name属性要是string
//不能有其他属性

Qualify some properties.

let abc:{
    
    
	name:string,
	[propName:string]:any
}
//可以随意加属性

function object

let abc: (a: number, b: number) => number;

abc = function (a: number, b: number) {
    
    
	return a + b;
}

array

an array of a certain type

let abc: string[];

//只能存string

Writing two:

let abc: Array<string>;

tuple

Array, but fixed length.

let abc: [string,number];

There can only be two elements, and the type is string, number in turn

enumerate

Only one of several values ​​can be selected.

enum Gender {
    
    
	Male,
	Female
}
let myGender: Gender;
myGender = Gender.Female

Semantics are better.

Guess you like

Origin blog.csdn.net/qq_37284843/article/details/123632532