What’s new in TypeScript 2.7

The new release of the JavaScript superset include fixed length tuples definite assignment checks for class properties

What’s new in TypeScript
Mira DeShazer (CC0)

Microsoft has released the production version of 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 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.

TypeScript 2.7 also is slated to add refactoring of the CommonJS module to ECMAScript 6, with this being the version of the standard JavaScript specification approved by ECMA International in 2015.

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.
  • Improvements to the pretty flag, 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.

“However, over time, we have seen two motivating examples,” Rosenwasser added, “migrating from JavaScript to TypeScript, and overcoming type checks that live in legacy code.” When migrating JavaScript to TypeScript, error suppression can help with a situation in which developers run into a pattern that is difficult to model. Developers could spend time trying to understand the pattern but may want to get of it later anyway. Now they can use suppression comments to avoid errors where code will still run fine.

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:

  • Tagged template strings in a module are now cached after the initial invocation. This move aligns TypeScript more closely with recent revisions to the ECMAScript specification underlying JavaScript. With this revision, developers can use libraries such as Lit-html and HyperHTML.
  • 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.