typescript study

  1. create-data: 2019-11-28 16:14:02
  2. author : yuyuanqiu
  3. version : v0.0.1-alpha

handbook

basic types

  • boolean
  • number
    • numbers are floating-point values
    • support hex, decimal, binary and octal
  • string
    • use double, single quotes
    • use backtick/backquote for template strings, and embed expressions using ${ expr }
  • array
    • using []
    • using generic array type like Array<elemType>
  • tuple
    • an array with a fixed number and known type of elements
    • elements need not be same
    • access an outside of indices fails with an error
  1. let isDone: boolean = false;
  2. let decimal: number = 6;
  3. let hex: number = 0xf00d;
  4. let binary: number = 0b1010;
  5. let octal: number = 0o744;
  6. let color: string = "blue";
  7. color = 'red';
  8. let fullName: string = `Bob Bobbington`;
  9. let age: number = 37;
  10. let sentence: string = `Hello, my name is ${ fullName }.
  11. I'll be ${ age + 1 } years old next month.`;
  12. // equivalent like so:
  13. let sentence: string = "Hello, my name is " + fullName + ".\n\n" +
  14. "I'll be " + (age + 1) + " years old next month.";
  15. let list: number[] = [1, 2, 3];
  16. let list: Array<number> = [1, 2, 3];
  17. // declare a tuple type
  18. let x: [string, number];
  19. // initialize it
  20. x = ["hello", 10]; // ok
  21. // initialize it incorrectly
  22. x = [10, "hello"]; // error
  23. console.log(x[0].substring(1)); // ok
  24. console.log(x[1].substring(1)); // error, "number" does not have "substring"
  25. x[3] = "world"; // error, property "3" does not exist on type "[string, number]".
  26. console.log(x[5].toString()); // error, property "5" does not exist on type "[string, number]".