TS data types and interfaces and object types

1. The data type of TS


  • TS is JS 超集, so JS basic types are included
  • Basic types: Boolean, Number, String, null, undefined with ES6Symbol 和BigInt
	npm install typescript -g
	tsc -w
	tsc --init
	npm i ts-node --g
	npm i @types/node -D
	ts-node ‘文件名’

1. String type

  • Strings are defined using string
// 普通声明
let a:string = '123';
console.log(a);

// ES6的字符串模板
let str:string = `ddd${
      
      a}`
console.log(str);

2. Number type:

  • Supports hexadecimal, decimal, octal and binary
let notANumber: number = NaN;           //Nan
let num: number = 123;                  //普通数字
let infinityNumber: number = Infinity;  //无穷大
let decimal: number = 6;                //十进制
let hex: number = 0xf00d;               //十六进制
let binary: number = 0b1010;            //二进制
let octal: number = 0o744;              //八进制s
console.log(notANumber,num,infinityNumber,decimal,hex,binary,octal);

3. Boolean type

let boolean: boolean = new Boolean(1)   // 报错,事实上new Boolean() 返回的是一个 Boolean 对象
  
let boolean1: Boolean = new Boolean(1)
let boolean2: boolean = true          //可以直接使用布尔值
let boolean3: boolean = Boolean(1)    //也可以通过函数返回布尔值
console.log(boolean1,boolean2,boolean3);

4. Null type

  • JavaScript does not have the concept of void (Void). In TypeScript, void can be used to represent a function without any return value
  • The void type is mainly used when we don’t want the caller to care about the return value of the function, such as the usual asynchronous callback function
function voidFn(): void {
    
    
  console.log('test void')
}
voidFn()
  • void can also define undefined and null types
// void也可以定义undefined和null类型 
let u: void = undefined
let n: void = null; // 严格模式下,null不能赋予void类型 
console.log(u);

5. Null and undefined types

let u: undefined = undefined; //定义undefined
let n: null = null;           //定义null
console.log(u, n);

6. null、undefined、void、never

  • null、undefined
    • The undefined type has only one value of undefined, and the null type has only one value of null.
    • undefined means undefined and null means missing value
  • void、never
    • In typescript, the void and never types have clear special functions.
    • void is the return type when a function doesn't explicitly return anything (e.g. consloe.log)
    • never is the type used when the function doesn't return at all (for example the function throws an exception, or runs forever).

insert image description here

2. Any type


1. Order of types

1. any和unknown是顶级类型
2. Number、String、Boolean
3. number、string、boolean
4. 1 、‘小青’、false
5. never

2. any (any type)

let any:any = 123
any = '123'
any = true
console.log(any);
  • No type is specified when declaring a variable默认为any
let any;
any = '123'
any = true
console.log(any);
  • Disadvantages If you use any, you will lose the role of TS type detection

3. unknown

 1. S中引入的unknown类型也被认为是顶级类型,但它更安全。 
 2. 与any一样,所有类型都可以分配给unknown
 3. unknow类型比any更加严格,当你要使用any 的时候,可以尝试使用unknow
  • unknown can define any type of value
let value: unknown;
value = true;             // OK
value = 42;               // OK
value = "Hello World";    // OK
value = [];               // OK
value = {
    
    };               // OK
value = null;             // OK
value = undefined;        // OK
value = Symbol("type");   // OK
  • Notice:
    • The unknown type can only be assigned to itself or any
let a: any = 1
let b: number = 5
a=b
b=a
let a: unknown = 1
let b: number = 5   //正确写法let b: any = 5 / let b: unknown = 5
a=b
b=a  //报错
  • unknown has no way to read any attribute methods and cannot be substituted
  • unknown is safer than any
let obj: any = {
    
    
  name: '张三',
  open: () => {
    
    
    console.log(obj.open());
  }
}

insert image description here

Three, Object, object and {}


1. Object

  • The Object type is the type of all instances of the Object class.
    • The Object interface defines properties on the Object.prototype prototype object
    • The ObjectConstructor interface defines the properties of the Object class, such as Object.create() mentioned above.
  • This type is related to the prototype chain. The top layer of the prototype chain is Object, so the value type and reference type eventually point to Object, so it contains all types.
let a1: Object = 123
let a2: Object = '123'
let a3: Object = []
let a4: Object = {
    
    }
let a5: Object = () => 123

2. object

  • Object representatives 所有非值类型的类型, such as arrays, objects, functions, etc., are often used for generic constraints
let a1: object = 123         // 错误 原始类型
let a2: object = '123'       // 错误 原始类型
let a6: object = false       // 错误 原始类型
let a3: object = []          // 正确
let a4: object = {
    
    }          // 正确
let a5: object = () => 123   // 正确

3.{}

  • Understand as new Object, including all types
  • Literal mode cannot modify the value
let a1: {
    
    } = 123             //正确
let a2: {
    
    } = '123'           //正确
let a3: {
    
    } = {
    
    name:'张三'}    //正确
let a4: {
    
    } =  () => 123       //正确

a3.age = 18 //报错 不能修改

4. Interface and Object Types

1. Interface

Function: In object-oriented transformation, interface is a specification definition, which defines the specification of behavior and action. In programming, interface plays a role of restriction and specification. Specification of behaviors and actions, constraining batch methods

  • Interface cannot have multiple attributes, nor can it have fewer attributes
interface Obj {
    
    
  name: string,
  age: number
}

let obj1: Obj = {
    
    
  name: '张三',
  age: 18
}
  • Interfaces with the same name will be merged
interface Obj {
    
    
  name: string,
  age: number
}
interface Obj {
    
    
  sex: string
}
let obj1: Obj = {
    
    
  name: '张三',
  age: 18,
  sex: '男'
}
  • interface any key
interface Obj {
    
    
  name: string,
  age: number
  [propName: string]:any
}

let obj1: Obj = {
    
    
  name: '张三',
  age: 18,
  a:1,
  b:2,
  c:3
}
  • interface ?和readonly
interface Obj {
    
    
  name: string,
  age?: number
}

let obj1: Obj = {
    
    
  name: '张三',
  age: 18
}
interface Obj {
    
    
  name: string,
  age?: number,
  readonly id: number,
  readonly cb: () => boolean
}

let obj1: Obj = {
    
    
  name: '张三',
  age: 18,
  id: 1,
  cb: () => {
    
    
    return false
  }
}
  • interface interface inheritance
interface Obj extends OBj2 {
    
    
  name: string,
  age?: number,
  readonly id: number,
  readonly cb: () => boolean
}
interface OBj2 {
    
    
  sex: string
}
let obj1: Obj = {
    
    
  name: '张三',
  age: 18,
  sex: '男',
  id: 1,
  cb: () => {
    
    
    return false
  }
}
  • interface defines the function type
interface Fn{
    
    
  (name:string): number[]
}
const Fn:Fn = function(name:string){
    
    
  return [1]
}

Guess you like

Origin blog.csdn.net/m0_58190023/article/details/129451878