# TypeScript 基本数据类型

// 字符串
let anExampleStr: string = "Hello World"
// 数字:可整数可浮点数
let anExampleNum: number  = 123
let anExampleNum2: number  = 123.345
// 布尔值
let anExampleNumBool: boolean = true
// 类型并集:比枚举更加灵活
let answer: 'yes'|'no'|'maybe' = 'maybe'
let httpStatus: 200 | 400 | 500 = 200
let right: 1 | true | 'yes' = true
answer = 'yes'
httpStatus = 400
// 函数
function f(s: 1 | true | 'yes') {
    let result: string | number | boolean = s
    console.log(result)
}
f(1)
f('yes')
f(true)
// 任意类型
let a : any = 'abc'
a = 12
a = false
a = 'hedon'
a = {}
a.name = 'Hedon'
// 可不写类型,编译器自动推断
let anExampleVariable = "hhh"
上次更新: 11/3/2021, 8:49:20 AM