What’s new in TypeScript 3.4

TypeScript 3.4 release candidate promises faster incremental builds, easier read-only arrays, and new support for read-only tuples

1 2 Page 2
Page 2 of 2
  • Pragma comments are supported for specifying the source of a JSX factory in a file. JSX is an embeddable XML-like syntax supported by TypeScript; it can be transformed into JavaScript.
  • Also, with JSX, the Element is resolved via the JSX factory. This capability is helpful when mixing and matching libraries such as React and Preact, or when mixing different versions of a library. At present, when JSX is used, TypeScript looks up a global JSX namespace for finding certain types. With Version 2.8, the compiler will try to look up the namespace based on the location of the JSX factory. Microsoft advises that going forward, new JSX-oriented libraries should avoid having JSX in the global scope. Instead, it should be exported from the respective factory function.
  • Granular control on mapped type modifiers enables more explicit syntax for adding and removing modifiers in mapped types with the + and operators.

Previous version: The features in TypeScript 2.7

Released in late January 2018, 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 adds 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.

Previous version: The 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.

Previous version: The features 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.

Copyright © 2019 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2
InfoWorld Technology of the Year Awards 2023. Now open for entries!