Upgrades to TypeScript 4.3
… and what to expect!
With TypeScript growing in popularity over the last few years, it should come to no surprise that developers of the language are always finding ways to upgrade and improve. Developed and maintained by Microsoft, TypeScript is gaining traction among developers for its readability and performance regarding larger projects, as well as its ability to be compiled into JavaScript (which makes learning TypeScript easy if you are already comfortable with JavaScript).
If you want a semi-comprehensive list of the difference between TypeScript and JavaScript, as well as which language would work best for your project, here is a link to a previous article I wrote discussing some of the differences between the two.
First, if you’d like to upgrade to the newest version of TypeScript, type the following command into your terminal:
npm install -g typescript
To double check which version you have, try this command:
tsc -v
What should we expect?
- Improvements in string types
- Improvements in compilation efficiency
- Truthy promise checks
- Autocomplete of import statement
- Smaller .tsbuildinfo files resulting in faster builds
In TypeScript 4.3, you can be specific when defining different types while reading and writing properties. Prior to this, you couldn’t specify these types which usually ended with errors. In addition to this, you can also add multiple types to your setter or getters.
For example:
class User {
get name(): string {
return this.name;
}
set name(value: string | boolean)}
You are now also able to use a keyword to override superclass methods. Now, TypeScript makes sure that if a method is overriden in a subclass, the same method name will remain in the master class.
It’s important to remember that forgetting the “override” keyword in the subclass after using in the superclass may also throw an error.
You can also use template string types to create new string types by combining patterns or other string types with a similar pattern.
type age = "45 year old" | "12 year old";
type height = "15 foot tall" | "38 foot tall";
type tree = '${ Age | Height} tree'
As with most upgrades, please double check to see how some of these changes may affect your legacy code. You may receive errors on previously functioning code.