123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153 |
- (()=>{
- //布尔类型
- let isDone = false
- // isDone = 10
- // isDone = 'true'
- // isDone = undefined
- // isDone = null
- console.log(isDone)
- //数字类型
- let a1 = 10 //二进制
- let a2 = 10 //十进制
- let a3 = 10 //八进制
- let a4 = 0xa //十六进制
- console.log(a1)
- console.log(a2)
- console.log(a3)
- console.log(a4)
- //字符串类型
- let name = 'tom'
- console.log(name)
- //字符串和数字之间能够进行拼接么?
- let str5 = '我有这么多钱'
- let num = 10000000
- console.log(str5 + num)
- //可以进行拼接 js的方式
- let und = undefined
- let nll = null
- console.log(und,nll)
- //默认情况下 null和undefined是所有类型的子类型
- //数组类型
- //数组类型定义方式1 元素类型后面接上[]
- let arr1: number[] = [1,2,3,4,5]
- let arr2: string[] = ['1','2','3']
- console.log(arr1)
- console.log(arr2)
- //数组类型定义方式2 使用数组泛型
- var arr3: Array<number> = [1,2,3,4]
- var arr4: Array<string> = ['1','2','3']
- console.log(arr3)
- console.log(arr4)
- //注意: 数组定义后,里面的数据的类型必须和定义数组时候的类型完全一致
- // var arr5: Array<number> = ['小明',123,true,null,undefined]
- let arr6 = ['小明',123,true]
- console.log(arr6)
- // console.log(arr6[3].split(''))
- //元组类型 (Tuple)
- let t1:[string,number,string] = ['小明',123,'小红']
- console.log(t1)
- // console.log(t1[0].split(''))
- // console.log(t1[1].split(''))
- //枚举
- //枚举里面的每个数据值都可以叫做元素 每个元素都有自己的编号 从0开始
- enum Color{
- Red = 1,
- Green = 3,
- Blue =7
- }
- let myColor: Color = Color.Blue
- let myColor1: string = Color[3]
- console.log(Color)
- console.log(myColor)
- console.log(myColor1)
- //any类型
- //在编程阶段 可能我们还不知道这个变量最终为什么类型 这个时候 赋值一个any类型
- let nnn: any = 100
- // nnn = 'xiaohong'
- nnn = true
- console.log(nnn)
- let xxx: any[] = [1,true,'xiaoming']
- console.log(xxx)
- //当一个数组种要存储多个数据类型 不确定的时候 可以使用any来定义数组
- //这种时候 没有任何错误的提示信息
- // console.log(xxx[0].split(''))
- //void 类型
- //某种意义上来说 any类型的反面 表示没有任何类型
- function showMsg(): void{
- console.log('hello word')
- }
- console.log(showMsg())
- //声明一个void变量没什么用 只能赋值 null undefined
- let aaa: void = undefined
- //object类型
- //定义一个object类型 参数是object类型 返回值也是object类型
- function getObj(obj:object): object{
- console.log(obj)
- return {
- name: 'xiaoming',
- age: 30
- }
- }
- console.log(getObj({name:'zs',age:18}))
- //联合类型 表示取值可以为多种类型
- //需求1:定义一个函数得到一个数组或者字符串
- // function toString(x: number | string): string{
- // return x.toString()
- // }
- // console.log(toString('123'))
- //需求2: 定义一个函数得到一个数组或者字符串的长度
- function toString(x: number | string): number{
- // return x.toString().length
- // if(x.length){
- // return x.length
- // } else {
- // return x.toString().length
- // }
- if((<string>x).length){
- return (x as string).length
- } else {
- return x.toString().length
- }
- }
- console.log(toString(123))
- //类型断言 告诉编译器 我知道我自己要什么类型
- /*
- 方式1: <类型>值
- 方式2: 值 as 类型
- */
- // 类型推断 TS会在没有明确指定类型的时候推测出一个类型
- // let c1 = 100
- // c1 = 'xiaoming'
- let c1
- c1 = 100
- console.log(c1)
- c1 = 'xiaoming'
- console.log(c1)
- })()
|