美文网首页
typescript入门-基础类型

typescript入门-基础类型

作者: copyLeft | 来源:发表于2021-06-07 17:10 被阅读0次

    boolean

    const status:boolean = false;
    

    number

    const int = 10
    const float = 1.1
    

    string

    const s_1 = '...'
    const s_2 = "..."
    const s_3 = '''
                  row 1
                  row 2
                '''
    

    array

    const list: number[] = [1, 2, 3]
    

    enum

    // 数字枚举
    enum T1 {
        SUCCESS,
        FAIL,
        WARNING
    }
    console.log(T1.SUCCESS)
    // 0
    
    
    // 设置初始值
    enum T2{
      SUCCESS = 2,
      FAIL,
      WARNING
    }
    
    
    console.log(T2.FAIL)
    // 3
    
    
    // 字符枚举, 字符枚举必须为每一项设置具体值
    enum Type {
      SUCCESS = 'success',
      FAIL = 'fail',
      WARNING = 'warning'
    }
    
    
    console.log(Type.FAIL)
    
    
    // 异构枚举, 如果前一项为字符项, 后一项为数字时,必须明确设置数值
    enum Enum {
      A,
      B,
      C = "C",
      D = "D",
      E = 8,
      F,
    }
    

    tuple

    const tp: [string, number] = ['msg', 10]
    

    any

    // any 可以为任意类型
    let cache:any = 1
    cache = 'msg'
    cahce = null
    

    unknown

    let uk: unknown
    let ay: any
    uk = 'msg'
    ay = uk
    // unknown 只能赋值给 any, unknown,不能赋值给其他具体的类型值
    const msg:string = uk
    // Type 'unknown' is not assignable to type 'string'.
    

    void

    let v1: void = undefined
    v1 = null
    //void 只能接收 undefined、 null
    

    null、 undefined、 never

    const n: null = null
    const u: undefined = undefined
    function empty():never{}
    

    相关文章

      网友评论

          本文标题:typescript入门-基础类型

          本文链接:https://www.haomeiwen.com/subject/nboleltx.html