Microsoft keeps updating TypeScript
Our expert's opinion
- Yoram Ceelen, Associate Consultant
What’s new in TypeScript 2.7
Microsoft has moved TypeScript 2.7 to release candidate stage, with general availability of the new version expected in mid-February 2018.
Where to download TypeScript
You can download TypeScript through NuGet or via NPM. To get the TypeScript 2.7 release candidate via npm, use the following command: npm install -g typescript@rc.
The new features in TypeScript 2.7
TypeScript 2.7 brings three noteworthy improvements: definite assignment checks for class properties, fixed length tuples, and improved narrowing for the in and instanceof operators.
With fixed length tuples, Microsoft notes, “tuple types now encode their arity [the number of elements they include] into the the type of their respective length property, and tuples of different types are no longer assignable to each other.” This is a breaking change.
Other new capabilities in TypeScript 2.7 include:
- Definite assignment checks for class properties. A new flag, --strictPropertyInitialization, performs checks to ensure that each instance property of a class is initialized in the constructor body or by a property initializer. A compiler option, this flag will be turned on with other --strict mode flags. To turn off this checking, developers can set the --strictPropertyInitialization setting to false in tsconfig.json’s compilerOptions, or --strictPropertyInitialization false on the command line.
- Improvements related to narrowing for in and instanceof operators. The in operator now acts as a narrowing expression for types, with types narrowed out that do not explicitly declare properties of a given name. The instanceof operator now leverages the inheritance chain, rather than relying on structural compatibility. This more accurately reflects how instanceof may behave at runtime.
- Definite assignment assertions, in which a variable or property declaration can include a definite assertion in the form of a “!” character after the variable or property identifier. This assertion instructs the control flow analyzer to consider the variable definitely assigned, even if the analyzer cannot prove it.
The new features in TypeScript 2.6
Released on October 2017, TypeScript 2.6 introduced a strict mode flag, which is identified as --strictFunctionTypes. In strict mode, any function type that does not originate from a method has its parameters compared “contravariantly,” said Microsoft’s Daniel Rosenwasser, program manager for TypeScript.
TypeScript traditionally compared parameters in a “bivariant” manner, allowing for benefits such as a simpler model for working with arrays. TypeScript 2.6 offers a compromise of increasing strictness on all function types except methods while letting TypeScript continue modeling use cases such as event handlers and simpler array handling.
Another key set of new features in TypeScript 2.6 covers error suppression comments, which are introduced via // @ts-ignore comments. Microsoft has avoided error suppression in TypeScript because most cases where users asked for it could be solved through more accurate declaration files, or using a type assertion to any, Rosenwasser said.
For overcoming type checks in legacy code, some large organizations update project dependencies in tandem. Any change that introduces a type-checking error requires a fix to avoid breaking a build. “While the error is usually useful, the reality of the situation is that the code has functioned thus far and teams have finite resources,” Rosenwasser said. Microsoft advises using suppression comments sparingly, and always with an explanation.
Also in TypeScript 2.6:
- The standalone TypeScript compiler now offers localized messages over NPM when using the --locale flag.
- The --watch mode, for emitting modules, has been made faster.
- Tool support improvements are planned for tools such as Visual Studio and Visual Studo Code, including quick fixes for anys, with TypeScript able to infer types for declarations with types that are an implicit any.
- Developers can refactor JSDoc documentation comments to TypeScript annotations.
- Editors can offer a quick fix to install type declarations for untyped imports.
- TypeScript 2.6 has some breaking changes, such as write only references being considered unused under --noUnusedLocals and --noUnusedParameters.
- Organizational changes have been made to DOM declarations in lib.d.ts.
in ambient contexts, such declaration files and declare module blocks, expressions are disallowed in default exports.
- Write-only references are now considered unused under --noUnusedLocals and --noUnusedParameters.
Uninhabitable types that result from intersections such as number and string (:"foo" & 42, for example) simplify to never when in a union.
What’s new in TypeScript 2.5
TypeScript 2.5, released in September 2017, includes an enhancement for try/catch statements for errors as well as compiler improvements.
The catch binding parameters capability in TypeScript 2.5 uses a late-stage ECMAScript feature to make catch binding optional in try/catch statements. Making catch binding optional “means we can just omit unusedError altogether,” said Rosenwasser. The reason for that is there are times when developers expect something might fail by throwing an error, but the developer does not care what the error is.
Also featured in TypeScript 2.5:
- Support for imports when using the Node module-resolution strategy. The compiler checks whether files originate from “identical” packages. If a file was derived from a package with a package.json containing name and version fields identical to a previously encountered package, TypeScript will redirect to the top-most package. This resolves a situation in which two packages might have identical declaration of classes but contain “private” members that make them structurally incompatible. A side effect of this change is reduction in memory and the runtime footprint of the compiler and language service.
- The preserveSymlinks compiler flag, which is like to the –preserveSymlinks flag in Node.js that instructs the module loader to preserve symbolic links when resolving and caching modules.
Other insights in ICT & Digital
Read our insight
COVID19 and IT: tracing apps and the risk privacy violations
Our expert's opinion