Appendix - Special characters in TS

  1. _ Number separator

    The delimiter _ does not change the value of a numeric literal, but the logical grouping makes it easier to read the numbers at a glance.

    1
    2
    const inhabitantsOfMunich = 1_464_301;
    const bytes = 0b1111_10101011_11110000_00001101;

    Usage restrictions:

    1. You can only add the _ separator between two numbers. The following example is illegal:

      1
      2
      3
      4
      5
      6
      3_.141592 // Error
      1_e10 // Error
      _126301 //Error
      126301_ // Error
      0_b111111000 // Error
      123__456 // Error, multiple _ delimiters cannot be used continuously.

    2. The function that parses numbers does not support separators.

      1
      2
      3
      Number('123_456') //NaN
      parseInt('123_456') //123
      parseFloat('123_456') //123

  2. #XXX Private Field

    ECMAScript private fields have been supported since TypeScript 3.8. Unlike regular properties (even properties declared with the private modifier), the rules are:

    1. Private fields start with the # character, sometimes we call them private names;
    2. Each private field name is uniquely limited to the class it contains;
    3. TypeScript accessibility modifiers (such as public or private) cannot be used on private fields;
    4. Private fields cannot be accessed outside the containing class, or even detected.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    class Person {
    #name: string; //Private field
    constructor(name: string) {
    this.#name = name;
    }
    greet() {
    console.log(`Hello, my name is ${this.#name}!`);
    }
    }

    let semlinker = new Person("Semlinker");
    semlinker.#name; // Property '#name' is not accessible outside class 'Person'.

    The difference between private fields and private:

    Private properties and methods can be accessed in some special ways. like:

    1
    2
    3
    4
    5
    6
    7
    class Person {
    constructor(private name: string){}
    }

    let person = new Person("Semlinker");
    console.log(person.name); //Property 'name' is private and only accessible within class 'Person'.
    console.log((person as any).name); //Can be accessed normally!

    This is because after private is compiled into ES5 code, it becomes:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    var Person = /** @class */ (function () {
    function Person(name) {
    this.name = name;
    }
    return Person;
    }());

    var person = new Person("Semlinker");
    console.log(person.name);

    The private fields are different after compilation.


  3. ! Non-null assertion (section 11)

  4. ?. Optional Chaining (Section 11)

  5. ?? Null value coalescing operator (Section 11)

  6. ?: optional attribute (section 9)

  7. Tool Type (Section 13)

  8. & intersection (Section 10)

  9. | union (Section 4)

  10. <type> Syntax: assertion (section 11) or generics (section 12)

Share