_ 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
2const inhabitantsOfMunich = 1_464_301;
const bytes = 0b1111_10101011_11110000_00001101;Usage restrictions:
You can only add the
_
separator between two numbers. The following example is illegal:1
2
3
4
5
63_.141592 // Error
1_e10 // Error
_126301 //Error
126301_ // Error
0_b111111000 // Error
123__456 // Error, multiple _ delimiters cannot be used continuously.
The function that parses numbers does not support separators.
1
2
3Number('123_456') //NaN
parseInt('123_456') //123
parseFloat('123_456') //123
#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:
- Private fields start with the # character, sometimes we call them private names;
- Each private field name is uniquely limited to the class it contains;
- TypeScript accessibility modifiers (such as public or private) cannot be used on private fields;
- Private fields cannot be accessed outside the containing class, or even detected.
1
2
3
4
5
6
7
8
9
10
11
12class 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
7class 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
9var 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.
! Non-null assertion (section 11)
?. Optional Chaining (Section 11)
?? Null value coalescing operator (Section 11)
?: optional attribute (section 9)
Tool Type (Section 13)
& intersection (Section 10)
| union (Section 4)
<type>
Syntax: assertion (section 11) or generics (section 12)