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{}