Discovering TypeScript 2.7— From pretty to prettier and from clean to cleaner
TypeScript 2.7 is here and it seems that developers have taken well into account pull requests for error fixing in numerous features while introducing new tools.
Now, without further ado, let’s dig in all the changes of the latest release.
According to the official release notes, the most significant changes and new additions are the following:
- Stricter class property checks
- Definite assignment assertions
- Easier ECMAScript module interoperability
- unique symbol types and const-named properties
- Cleaner output in –watch mode
- Prettier –pretty output
- Numeric Separators
- Fixed Length Tuples
- in operator narrowing and accurate instanceof
- Smarter object literal inference
But let’s have a closer look at some of the features.
Stricter class property checks
The new strictness flag named
-- strictPropertyInitialization makes sure that each instance property of a class gets set in the constructor body. However, you should pay attention when initializing
--strictPropertyInitialization since it will be turned on along with other
-- strict mode flags that can impact the whole project.
Easier ECMAScript module interoperability
As part of the endeavor to find the best way to interoperate with existing “legacy” module formats, TypeScript introduces the new
-- esModuleInterop flag for callable Common JS modules. Under the new feature, these modules must be imported as default imports, as seen below:
import express from "express"; let app = express();
Cleaner and prettier
As mentioned before, TypeScript developers had a closer look into request and feedback and incorporated the suggested error fixes and improvements to the best of their ability. To give some examples,
-- watch mode now clears the screen after requesting a re-compilation while
-- pretty flag now uses colors for file names, diagnostic codes and line numbers as well as fine names and positions are now formatted in a manner that common terminals can allow a Ctrl+Click, Cmd+Click, Alt+Click, etc. to move to the appropriate location in the editor.#
in operator narrowing and accurate instanceof
Two changes are featured in operator narrowing in 2.7. In order to help avoid complex issues when using
instanceof to narrow from structurally similar, though unrelated types, the
instanceof operator now leverages the inheritance chain instead of relying on structural compatibility while the
in operator now acts a type guard to narrow out types that do not explicitly declare properties of a given name.