1_基本数据类型.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. (()=>{
  2. //布尔类型
  3. let isDone = false
  4. // isDone = 10
  5. // isDone = 'true'
  6. // isDone = undefined
  7. // isDone = null
  8. console.log(isDone)
  9. //数字类型
  10. let a1 = 10 //二进制
  11. let a2 = 10 //十进制
  12. let a3 = 10 //八进制
  13. let a4 = 0xa //十六进制
  14. console.log(a1)
  15. console.log(a2)
  16. console.log(a3)
  17. console.log(a4)
  18. //字符串类型
  19. let name = 'tom'
  20. console.log(name)
  21. //字符串和数字之间能够进行拼接么?
  22. let str5 = '我有这么多钱'
  23. let num = 10000000
  24. console.log(str5 + num)
  25. //可以进行拼接 js的方式
  26. let und = undefined
  27. let nll = null
  28. console.log(und,nll)
  29. //默认情况下 null和undefined是所有类型的子类型
  30. //数组类型
  31. //数组类型定义方式1 元素类型后面接上[]
  32. let arr1: number[] = [1,2,3,4,5]
  33. let arr2: string[] = ['1','2','3']
  34. console.log(arr1)
  35. console.log(arr2)
  36. //数组类型定义方式2 使用数组泛型
  37. var arr3: Array<number> = [1,2,3,4]
  38. var arr4: Array<string> = ['1','2','3']
  39. console.log(arr3)
  40. console.log(arr4)
  41. //注意: 数组定义后,里面的数据的类型必须和定义数组时候的类型完全一致
  42. // var arr5: Array<number> = ['小明',123,true,null,undefined]
  43. let arr6 = ['小明',123,true]
  44. console.log(arr6)
  45. // console.log(arr6[3].split(''))
  46. //元组类型 (Tuple)
  47. let t1:[string,number,string] = ['小明',123,'小红']
  48. console.log(t1)
  49. // console.log(t1[0].split(''))
  50. // console.log(t1[1].split(''))
  51. //枚举
  52. //枚举里面的每个数据值都可以叫做元素 每个元素都有自己的编号 从0开始
  53. enum Color{
  54. Red = 1,
  55. Green = 3,
  56. Blue =7
  57. }
  58. let myColor: Color = Color.Blue
  59. let myColor1: string = Color[3]
  60. console.log(Color)
  61. console.log(myColor)
  62. console.log(myColor1)
  63. //any类型
  64. //在编程阶段 可能我们还不知道这个变量最终为什么类型 这个时候 赋值一个any类型
  65. let nnn: any = 100
  66. // nnn = 'xiaohong'
  67. nnn = true
  68. console.log(nnn)
  69. let xxx: any[] = [1,true,'xiaoming']
  70. console.log(xxx)
  71. //当一个数组种要存储多个数据类型 不确定的时候 可以使用any来定义数组
  72. //这种时候 没有任何错误的提示信息
  73. // console.log(xxx[0].split(''))
  74. //void 类型
  75. //某种意义上来说 any类型的反面 表示没有任何类型
  76. function showMsg(): void{
  77. console.log('hello word')
  78. }
  79. console.log(showMsg())
  80. //声明一个void变量没什么用 只能赋值 null undefined
  81. let aaa: void = undefined
  82. //object类型
  83. //定义一个object类型 参数是object类型 返回值也是object类型
  84. function getObj(obj:object): object{
  85. console.log(obj)
  86. return {
  87. name: 'xiaoming',
  88. age: 30
  89. }
  90. }
  91. console.log(getObj({name:'zs',age:18}))
  92. //联合类型 表示取值可以为多种类型
  93. //需求1:定义一个函数得到一个数组或者字符串
  94. // function toString(x: number | string): string{
  95. // return x.toString()
  96. // }
  97. // console.log(toString('123'))
  98. //需求2: 定义一个函数得到一个数组或者字符串的长度
  99. function toString(x: number | string): number{
  100. // return x.toString().length
  101. // if(x.length){
  102. // return x.length
  103. // } else {
  104. // return x.toString().length
  105. // }
  106. if((<string>x).length){
  107. return (x as string).length
  108. } else {
  109. return x.toString().length
  110. }
  111. }
  112. console.log(toString(123))
  113. //类型断言 告诉编译器 我知道我自己要什么类型
  114. /*
  115. 方式1: <类型>值
  116. 方式2: 值 as 类型
  117. */
  118. // 类型推断 TS会在没有明确指定类型的时候推测出一个类型
  119. // let c1 = 100
  120. // c1 = 'xiaoming'
  121. let c1
  122. c1 = 100
  123. console.log(c1)
  124. c1 = 'xiaoming'
  125. console.log(c1)
  126. })()