05any
# any
- 任意值any用来表示允许赋值为任意类型
//不会报错
let num:any = 1
num = "hello"
1
2
3
2
3
- 在任意值上访问任何属性和调用任何方法都是允许的
//不会报错
let num:any = 1
console.log(num.aaa)
console.log(num.aaa.bbb)
num.hello()
1
2
3
4
5
2
3
4
5
- 变量在未指定类型的时候,默认为任意类型
let a
a = "hello"
a = 1
//等同于
let a:any
a = "hello"
a = 1
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
任意类型不能乱用,否则失去强制类型的意义。
编辑 (opens new window)
上次更新: 2023/08/06, 00:38:41