1. <template>
    2. <div>TypeScript typeof 操作符</div>
    3. <div>
    4. {{ lolo }}
    5. </div>
    6. <div>
    7. {{ aa }}
    8. </div>
    9. <div>
    10. {{ color }}
    11. </div>
    12. <div>
    13. {{ COLORS }}
    14. </div>
    15. </template>
    16. <script lang="ts">
    17. import { defineComponent } from "vue";
    18. interface Person {
    19. name: string;
    20. age: number;
    21. }
    22. const kakuqo = {
    23. name: "kakuqo",
    24. age: 30,
    25. address: {
    26. provice: "河南",
    27. city: "信阳",
    28. },
    29. };
    30. const COLORS = {
    31. red: "xx",
    32. blue: "blue",
    33. };
    34. export default defineComponent({
    35. setup() {
    36. // demo1
    37. const sem: Person = { name: "semgaga", age: 25 };
    38. // 获取sem的变量类型 并且赋值给 Sem
    39. type Sem = typeof sem;
    40. const lolo: Sem = { name: "lolo", age: 5 };
    41. // demo2
    42. type Kakuqo = typeof kakuqo;
    43. // demo3
    44. type Colors = keyof typeof COLORS;
    45. let color: Colors;
    46. color = "blue";
    47. // console.log("tag", color);
    48. let aa: Kakuqo = {
    49. name: "aa",
    50. age: 20,
    51. address: {
    52. provice: "山西",
    53. city: "临汾",
    54. },
    55. };
    56. return {
    57. lolo,
    58. aa,
    59. color,
    60. COLORS,
    61. };
    62. },
    63. });
    64. </script>
    65. <style>
    66. </style>

    参考链接:http://www.semlinker.com/ts-typeof/