原始数据类型有哪些

其实和javaScript是一致的

  • Boolean布尔类型
  • Null空对象
  • Undefined未定义
  • Number数值
  • BigIntes6新增对象
  • String字符串
  • Symboles6新增对象

    声明实例:

    例子

    在声明变量的时候,typeSript和javaScript会有一些不同点。 ```typescript let isBoolean: boolean = false

let age: number = 10 let num: number = undefined

let firstName: string = ‘chenYongRen’ let lastName: string = hello,${firstName}

let u: undefined = undefined let n: null = null

  1. <a name="Q0aqb"></a>
  2. #### Null和undefined的区别
  3. :::warning
  4. **定义:** **null表示"没有对象",即该处不应该有值,可以理解成是一个空的对象**
  5. :::
  6. > **典型用法:**
  7. > 1、作为函数的参数,表示该对象的参数不是对象。
  8. > 2、作为对象原型的终点。
  9. > 3、赋值给对象,释放内存
  10. ---
  11. :::warning
  12. **定义:** **undefined表示 "缺少值", 就是此处应该赋值,但是还没有定义。**
  13. :::
  14. > **典型用法**
  15. > 1、变量被声明了,但是没有赋值,就等于undefined
  16. > 2、调用函数时,应该提供的参数没有提供,该参数等于undefined
  17. > 3、对象没有赋值的属性,该属性的指为undefined
  18. > 4、函数没有返回值时,默认返回undefined
  19. ---
  20. <a name="SE15J"></a>
  21. ## Any类型
  22. > 上面的是原始数据类型的声明方式,
  23. > 如果有这么一个场景,通过接口返回的数据,它里面的数据类型我们不能提前知道,那怎么声明变量呢。
  24. :::warning
  25. **定义:允许赋值为任意类型,可以访问任何属性和方法**
  26. :::
  27. > **注意:如果有明确的类型应避免使用这个类型**
  28. <a name="evezD"></a>
  29. #### 例子
  30. ```typescript
  31. let notSure:any = 4
  32. notSure = 'maybe a string'
  33. notSure = true
  34. notSure.myName
  35. notSure.getName()

注意事项

💡 我们在声明类型的时候,类型尽可能用小写。