What’s new in TypeScript 2.7
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.
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
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
strictmode flags. To turn off this checking, developers can set the
strictPropertyInitializationsetting to false in t
--strictPropertyInitializationfalse on the command line.
- Improvements related to narrowing for
inoperator now acts as a narrowing expression for types, with types narrowed out that do not explicitly declare properties of a given name. The
instanceofoperator now leverages the inheritance chain, rather than relying on structural compatibility. This more accurately reflects how
instanceofmay 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.
- Improvements to the
prettyflag, for making error messages easier to read. TypeScript 2.7 adds the use of colors for file names, diagnostic codes, and line numbers. Also, file names and positions are formatted so common terminals can use Ctrl-click, Cmd-click and Alt-click to jump to the appropriate location in a developer’s editor.
- Support for ECMAScript numeric separators, for placing separators between digits to distinguish between digit groups.
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
--watchmode, 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
- 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 —
- Organizational changes have been made to DOM declarations in lib.d.ts.
- in ambient contexts, such declaration files and
declare moduleblocks, expressions are disallowed in
- Write-only references are now considered unused under
- Uninhabitable types that result from intersections such as number and string (
:"foo" & 42, for example) simplify to
neverwhen in a union.
What’s new in TypeScript 2.5
TypeScript 2.5, released in September 2017, includes an enhancement for
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
catchstatements. 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.jsoncontaining 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.
preserveSymlinkscompiler flag, which is like to the
–preserveSymlinksflag in Node.js that instructs the module loader to preserve symbolic links when resolving and caching modules.