What's new in Typescript 4.3 Release
Typescript 4.3 is now available to use, we can upgrade Typescript to latest version easily from the Older Typescript version using below "npm install" command, know what's new in Typescript 4.3, upgrading to Typescript latest version will help to use the new features introduced in the release and will help to improve the application's performance.
How to Upgrade Typescript older Version to Typescript 4.3:
npm install -g typescipt
OR run following command:
npm install -g typescript@4.3
Now, check the Typescript Version installed on the machine using following command:
tsc -v
Below are the new Features released in Typescript 4.3:
1. Separate Write Types on Properties:
Typescript 4.3 allows to specify different types on read and write of the properties. Before Typescript 4.3 it was not allowed to specify different types for get and set accessors of properties, it used to give error as: "'get' and 'set' accessors must have the same type", now in Typescript 4.3 we can specify different read & write types on properties as shown below:
- class Profile{
- #profileId= 0;
- get profileId(): number {
- return this.#profileId;
- }
- //Allows to specify Multiple Write Types in the Set accessors
- set profileId(value: string | number | boolean) {
- let id= Number(value);
- // Check for NaN.
- if (!id) {
- this.#profileId= 0;
- return;
- }
- this.#profileId= id;
- }
}
2. Override and the -noImplicitOverride flag:
Before Typescript 4.3, there was no "override" keyword to override a super class method, Now after Typescript 4.3, if a method marked with override in the child class, Typescript makes sure that the same method name should exist in the Parent class, otherwise it will throw an error.
- class Employee{
- displayName() {
- console.log('employee class');
- }
- }
- class Manager extends Employee{
- override displayName() {
- console.log('manager class');
- }
- }
Typescript 4.3 also provides a new flag : "--noImplicitOverride", if it is turned on, then it will give an error if you are trying to override the superclass method, but forgot to mention "override" keyword in the child class. So make sure you use override keyword in the child class if you are tying to override the super class method.
Now if we remove displayName() method from Employee class, it will give an error as : "This member cannot have an 'override' modifier because it is not declared in the base class 'Employee' "
3. Template string type improvements:
Template string type either construct new string like types by concatenating OR match pattern of other string like types:
- type color = "yellow" | "red";
- type quantity= "one" | "two";
- type car = '${Quantity | Color} car'; //It is same as : type car= "one car" | "two car" // "red car" | "blue car";
or match pattern of other string-like types as shown below:
declare let val1: '${number}-${number}-${number}'; declare let val2: '100-200-300' // Works! val1= val2;
4. ECMAScript #private class elements:
Typescript 4.3 allows properties, methods and accessors to set as private to restrict access to those fields outside the class. We can make the fields private using "#". In below example, we have made the method #validateName() as private, we have made #getId() reading accessors as private which restricts the use of these fields outside the class and gives an error, but it can be accessed inside the clas.
- class Employee {
- #validateName() {
- //...
- }
- get #getId() {
- return 10;
- }
- displayName() {
- // This works, because we can access private fields inside the class.
- this.#validateName();
- return this.#getId;
- }
- }
- new Employee ().#validateName();
- // error!
- // Property '#validateName' is not accessible
- // outside class 'Employee ' because it has a private identifier.
- new Employee ().#getId;
- // error!
- // Property '#getId' is not accessible
- // outside class 'Employee ' because it has a private identifier.
5. ConstructorParameters Works on Abstract Classes:
It is now possible to add ConstructorParameters on the abstract class and it works after Typescript 4.3 release. Below example shows the abstract class with ConstructorParameters:
- abstract class Employee {
- constructor(name: string, id: number) {
- }