1. The original type

1.string 2.number 3.null 4.boolean 5.undefined 6.symbol
    const a: string = '123'
    const b: number = 123
    const c:boolean = false
    const d:null = null
    const e:undefined = undefined
    const f:Symbol = Symbol(a)Copy the code
  1. The object type

1.object 2.Array 3.function
    // The object type is not only a value object, it is all types except primitive types
    
    const nfc1: object = () = >{}      // Can be a function
    const nfc2: object =  new Array  // Can array
    const nfc3: object =  new Object // Can be an object
Copy the code
  1. Is an object restriction type

    //1. Limit each parameter directly
    const obj1: { name: string,id: number } = { name:'Joe'.id:1 }
    //2. Use interface restrictions separately
Copy the code
  1. Is an array constraint type

    //1. Use the Array generic
    const arr1: Array<number> = [1.2.3]  // Each item in the array can only be a number
    const arr2: number[] = [1.2.3]       / / same as above
    //2
    const arr3: [number,string,boolean] = [1.'2'.true]
Copy the code
  1. Enumerated type

enum DataStatus{
    status1 = 1,
    status2 = 2,
    status3 = 3
}
const data = {
  name:'bill'.id:0.status:DataStatus.status1
}
Copy the code
  1. Function types

    //1. The function declares type constraints
    function fn1(a: number,b: string) :string{
        return a+The '-'+b  // The return value must be string
    }
    fn1(1.'2')// The parameters and arguments must have exactly the same number
    // If optional parameters are required, add? Can be
    function fn2(a: number,b: string,c? : boolean) :string{
        return a+The '-'+b  // The return value must be string
    }
    fn2(1.'2')// This parameter can be used without c
    
    //2. Function expression
    const fn3:(a: number,b: number) = >number = (a,b) = >{
      return a+b
    }
    fn3(1.2)
Copy the code
  1. Any type any

    // Use any to set parameter A to all types
    const fn3 = (a: any) = >{
      return a
    }
    fn3(1)
    fn3('1')
    fn3(false)
Copy the code
  1. Implicit type inference

    // select * from 'ts' where' ts' = 'num' and 'ts' =' number '
    let num = 1
    num = 100  // It's ok to reassign num
    num = '100'  // An error is reported for assigning a string
Copy the code
  1. Types of assertions

    const resolve = [ 100.200.300 ]
    const res = resolve.find(n= >n>1) // in this case, ts thinks that the value of res might be number or undefined
    const res1 = res as number  // The value of res1 must be number
Copy the code
  1. Interface interfaces

    //1. Static properties
    interface Todo{
      id: number,
      name: string, checked? : boolean// Optional membersreadonly title? : string// Here the title is restricted to read-only and cannot be changed
    }
    // Define the object directly
    const todo: Todo= { id:1.name:'ha ha' } // You may not pass checked
    todo.title = 'title' // If the title is readonly, an error will be reported
    // as an argument
    const setTodos = (todo: Todo) = >{
      return todo
    }
    
    //2. Dynamic properties
    interface Toos{
      [key:string]:string // Limit the type of the key object to string
    }
    const toos: Toos = {}
    toos.name = 'hello'
    toos.sex = 'male'
Copy the code
  1. The generic

Types that cannot be identified at definition time are passed as arguments when used
//T stands for string and U stands for number
const getTodos = <T,U>(list: T[],num:U): T[] =>{
  return list
}
const res = getTodos<string,Number>(['1','2','3'],U)
Copy the code