Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.52 KB | None | 0 0
  1. TypeScript
  2.  
  3.  
  4.  
  5. Feature
  6.  
  7.  
  8.  
  9. Basic Type
  10.  
  11. boolean
  12.  
  13. false and true.
  14.  
  15. let a: boolean = false;
  16.  
  17.  
  18.  
  19.  
  20.  
  21. number
  22.  
  23. let decimal: number = 6;
  24. let hex: number = 0xf00d;
  25. let binary: number = 0b1010;
  26. let octal: number = 0o744;
  27.  
  28.  
  29.  
  30. string
  31.  
  32. let color: string = "blue";
  33. color = 'red';
  34.  
  35. array
  36.  
  37. let list: number[] = [1, 2, 3];
  38. let list: Array<number> = [1, 2, 3];
  39.  
  40.  
  41.  
  42. tuple
  43.  
  44. // Declare a tuple type
  45. let x: [string, number];
  46. // Initialize it
  47. x = ["hello", 10]; // OK
  48. // Initialize it incorrectly
  49. x = [10, "hello"]; // Error
  50.  
  51. enum
  52.  
  53. enum Color {Red, Green, Blue}
  54. let c: Color = Color.Green;
  55.  
  56. any
  57.  
  58. let isDone: any = false;
  59. isDone.ifItExists()
  60.  
  61. Why we need any? Would not specifying any word works? No. If without any the compiler shows that ifItExists is not a method of isDone.
  62.  
  63. This is a kind of compatible design? To get people comfortable.
  64.  
  65. void
  66.  
  67. It's like the opposite of any, that the variable cannot have any type. Useful in function return value.
  68.  
  69. function warnUser(): void {
  70. console.log("This is my warning message");
  71. }
  72.  
  73. let a: void = 1; //error
  74. let a: void = undefined; //pass
  75.  
  76. undefined & null
  77.  
  78. These two are actually a type???
  79.  
  80. It's in advanced topic chapter.
  81.  
  82.  
  83.  
  84. never
  85.  
  86. Never be assigned.
  87.  
  88. let a:never;
  89. a = 2; // wrong
  90.  
  91. It's useful in function return type
  92.  
  93. // Function returning never must have unreachable end point
  94. function error(message: string): never {
  95. throw new Error(message);
  96. }
  97.  
  98. object
  99.  
  100. Representing non-primitive type
  101.  
  102. Type Inference
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement