typescript入门-基础类型

2021-06-11 18:12:25 浏览数 (1)

boolean

代码语言:javascript复制
const status:boolean = false;

number

代码语言:javascript复制
const int = 10
const float = 1.1

string

代码语言:javascript复制
const s_1 = '...'
const s_2 = "..."
const s_3 = '''
              row 1
              row 2
            '''

array

代码语言:javascript复制
const list: number[] = [1, 2, 3]

enum

代码语言:javascript复制
// 数字枚举
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

代码语言:javascript复制
const tp: [string, number] = ['msg', 10]

any

代码语言:javascript复制
// any 可以为任意类型
let cache:any = 1
cache = 'msg'
cahce = null

unknown

代码语言:javascript复制
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

代码语言:javascript复制
let v1: void = undefined
v1 = null
//void 只能接收 undefined、 null

null、 undefined、 never

代码语言:javascript复制
const n: null = null
const u: undefined = undefined
function empty():never{}

0 人点赞