typeScript学习-TS类型-字面量数据类型

空白格k / 2023-08-11 / 原文

typeScript学习

字面量数据类型:

// type A = number | string
// let a:A = "abc"
// type num = number
// let n:num = 3
type num = 1 | 2 | 3
let n:num = 2
// 错误情况
// let n:num = 4

type increaseFlag = 0 | 1
function isStartUp(increase:increaseFlag){
  if(increase){
    console.log('open')
  }else{
    console.log('close')
  }
}
isStartUp(0)
// 错误情况
// isStartUp(2)