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

The production version of TypeScript 3.4, the latest version of Microsoft’s typed superset of JavaScript, has arrived, with improvements for builds and type-checking.

Where to download TypeScript 

You can download TypeScript through NuGet, or you can get it via NPM:

npm install -g typescript

Current version: The new features in TypeScript 3.4

  • A new compilation flag, --incremental, provides faster subsequent builds. This flag tells TypeScript to save information about the project graph from the last compilation. When TypeScript is again invoked with --incremental, it will use the data to detect the least costly way to type-check and emit project changes.
  • Type-checking is introduced for the ECMAScript globalThis global variable, providing a standard way for accessing the global scope that can be used across different environments.
  • It is now easier to read-only array types, with a new syntax for ReadonlyArray using a Readonly modifier for array types.
  • Support is introduced for read-only tuples; any tuple type can be prefixed with the readonly keyword to make it a read-only tuple.
  • The readonly modifier in a mapped type automatically will convert array-like types to a corresponding read-only array type. 
  • A new construct has been introduced for literal values, const assertions. The syntax is a type assertion with const in place of the type name. When literal expressions are constructed with const assertions, developers can signal that no literal type in that expression should be widened, that object literals get readonly properties, and that array literals become readonly tuples.
  • As a breaking change, the type of top-level this is now typed as typeof globalThis instead of any. As a consequence, developers might receive errors for accessing unknown values on this and noImplicitAny.
  • Another breaking change is that improved inference in TypeScript 3.4 might produce generic functions rather than functions that take and return constants.

Previous version: The new features in TypeScript 3.3

Released in late January 2019, TypeScript 3.3 has incremental file watching for composite projects, enabling faster builds by rechecking and re-emitting only files that have changed, rather than performing a complete build of the entire project. The --watch flag of --build mode automatically takes advantage of this new feature. In tests, the --build --watch functionality resulted in a 50 percent to 75 percent reduction in build times compared to the original --build --watch.

TypeScript 3.3 also features improved behavior for calling union types. Also, the TypeScript plug-in for Sublime Text now supports editing in JavaScript files. 

Previous version: The new features in TypeScript 3.2

Released in November 2018, TypeScript 3.2 introduces stricter checking on the apply, bind, and call methods. These are methods on functions enabling capabilities such as binding this and partially applying arguments and call functions with a different value for this. Also, call functions can be fitted with an array for arguments.

Earlier versions of TypeScript lacked the power to model these functions; applybind, and call were all typed to take any number of arguments and return any. Also, ECMAScript 2015’s arrow functions and rest/spread arguments have provided a new syntax to make it easier to express what some of these methods do.

With the changes, bind, call, and apply are more strictly checked with the use of a flag, called strictBindCallApply. When this flag is used, methods on callable objects are described by a new global type, CallableFunction, that declares stricter signatures for bindcall, and apply. Also, any methods on objects that are construct-able but not callable are described by a new global type, NewableFunction.

Other new features in TypeScript 3.2 include:

  • Type-checking is enabled for BigInt as well as support for emitting BigInt literals when targeting esnext. The new primitive type bigint can be accessed by calling the BigInt() function or by writing out a BigInt literal by adding an nto the end of any integer numeric literal. Also, bigints produce a new string type when using the typeof operator: the string bigint. BigInt support is only available for the esnext target.
  • Object spreads are permitted on generics and are modeled using intersections. JavaScript supports copying properties from an existing object into a new one called “spreads.”
  • Object rest on generic types is featured, in which object rest patterns create a new object that lacks specified properties.
  • For d.ts, certain parameters no longer accept null. Also, some WebKit-specific properties have been deprecated.

Version 3.2 and future releases will only ship as an MSBuild package and not a standalone compiler package. The MSBuild package depends on the presence of a globally invokable version of Node.js. Computers with Visual Studio 2017 Version 15.8 and later have that Node.js version. TypeScript 3.2 is the last version with editing support for Visual Studio 2015.

Previous version: The new features in TypeScript 3.1

Released in September 2018, TypeScript 3.1 adds properties on function declarations. Thus, for any function or const declaration that is initialized with a function, the type-checker analyzes the containing scope to track added properties. This change is intended to make TypeScript smarter about patterns.

With JavaScript, functions are objects, with developers able to tack properties on them. TypeScript’s traditional approach to this has been through the namespaces construct. But this construct has not aged well. ECMAScript modules have become the preferred mode for organizing new code in TypeScript and JavaScript, but namespaces are TypeScript-specific. Also, namespaces do not merge with var, let, or const declarations, which can make code conversions difficult. These issues can make it tougher to migrate to TypeScript.

Other new features in TypeScript 3.1 include:

  • Version redirects for typesVersions, a field in a JSON file that tells TypeScript to check whether the current version of TypeScript is running. This impacts Node module resolution and addresses a situation in which library maintainers have had to choose between supporting new features and not breaking older versions of TypeScript. Developers of TypeScript cited an example in which a library is being maintained that uses the unknown type from TypeScript 3.0; any consumers using earlier versions would be broken. The new capability provides a way to provide types for pre-3.0 versions of TypeScript while also providing types for later versions.
  • A refactoring to convert promises that return promises constructed with chains of .()then and .catch() calls to async functions that use await.
  • Mapped tuple and array types are featured, with mapped types such as Partial or Required from d.ts now automatically working on tuples and arrays.
  • TypeScript 3.1 generates d.ts and other built-in declaration file libraries using Web IDL files from the WHATWG DOM specification. This means lib.d.ts will be easier to keep current but many vendor-specific types have been removed. This change can break existing code.
  • The use of the typeof foo === "function" type guard could also break existing code, providing different results when intersecting with questionable union types composed of {}, Object, or unconstrained generics.

Previous version: What’s new in TypeScript 3.0

Key to TypeScript Version 3.0, released in late July 2018, is a project references capability, which lets projects depend on other projects. With this capability, tsconfig.json files can reference other tsconfig.json files. By specifying these dependencies, it becomes easier to split code into smaller projects, with TypeScript and tools able to understand build order and output structure. As a result, builds become faster. Developers also gain support for transparently navigating and editing across projects.

Other new features in TypeScript 3.0 include:

  • Project references allow mapping of an input source to outputs.
  • A set of APIs for project references has been added. These references should in the future be able to integrate with a selection of build orchestrators.
  • Improved error messages provide guidance so developers can better understand the cause and effect of an error.
  • Richer tuple types, so tuples can model parameter lists. Previously, TypeScript could only model the order and count of a parameters set.
  • Support for a type alias in the JSX namespace, with LibraryManagedAttributes acting as a helper to tell TypeScript what attributes a JSX tag accepts. This helps with using TypeScript with the React JavaScript UI library, enabling modeling of React behavior for capabilities such as React’s defaultProps property, for filling in values for props that are omitted.
  • The unknown type, for describing the least-capable type in JavaScript. This is useful for APIs to signal that a type can be of any value and that type-checking must be performed before use. Returned values must be safely introspected. As a result, the unknown type no longer can be used in type declarations such as interfaces, type aliases, or classes.
  • Two productivity features are added for JSX, providing completions for JSX closing tags and collapsible outlining spans.
  • Named import refactorings, to help developers switch back and forth from qualifying imports with the modules they came from and individual imports.
  • Quick fixes to remove unreachable code and unused labels.
  • The deprecated internal method LanguageService#getSourceFile has been removed. So have the deprecated function TypeChecker#getSymbolDisplayBuilder and associated interfaces, as well as the deprecated functions escapeIdentifier and unescapeIdentifier.

Previous version: The new features in TypeScript 2.9

The release candidate released in June 2018 features support for object literals and numeric types, via the keyof operator and mapped object types. Keyof, which already is part of TypeScript, provides a way to query property names of an existing type. But this operator has predated TypeScript’s ability to reason about unique symbol types, so it never recognized symbolic keys. TypeScript 2.9 changes the behavior of keyof to factor in unique symbols and literal types. Also, mapped object types such as Partial and Readonly can recognize symbolic and numeric property keys; properties named by symbols will no longer be dropped.

Also in TypeScript 2.9 are:

  • Properties can be converted to get- and set- accessors.
  • An unused span reporting capability enables two lint-like flags, --noUnusedLocals and –noUnusedParameters, to be surfaced as “unused” suggestion spans.
  • Declarations can be moved to their own files and files can be renamed within a project while keeping import paths current.
  • The --pretty mode, to provide a fuller console experience, is now the default mode when TypeScript can figure out that it is printing output to a terminal. It can be turned off, however.
  • Type arguments can be placed on tagged template strings.
  • The import(…) type syntax, to addresses a shortcoming in which TypeScript cannot reference a type in another module, or the type of module itself, without including an import at the top of the file.
  • Support for passing of generics to JSX elements and passing generics to tagged template calls.
  • Adding suggestion diagnostics for open files.
  • Showing unused declarations as suggestions.
  • Resolving module names with modulename.json to JSON files when resolving node modules.

TypeScript 2.9’s changes can break existing code. Microsoft has cited these issues to be aware of:

  • Where developers have assumed that for any type T that keyof T always is assignable to a string. Symbol- and numeric-named properties invalidate this assumption. There are workarounds, such as using Extract<keyof T, string> to restrict symbol and number. Developers also can revert to old behavior under the --keyofStringsonly compiler flag, which is meant as a transitionary flag.
  • Trailing commas are not permitted on REST parameters; this was done for conformance with the ECMAScript specification for JavaScript.
  • Unconstrained type parameters can no longer be assigned to object in strictNullChecks.
  • Values of type never no longer can be iterated over, which might be useful in catching a class of bugs. This behavior can be avoided through use of a type assertion to cast to the type, any.

Previous version: The new features in TypeScript 2.8

Released in March 2018, Version 2.8 of TypeScript adds a conditional types construct for modeling. Based on JavaScript’s conditional syntax, conditional types help with modeling of simple choices based on types at runtime while allowing more expressive design-time constructs. The construct takes the following form: A extends B ? C : D. It should be read as “If the type A is assignable to B, then the type boils down to C and otherwise becomes D.” Conditional types also offer a new way to infer new types from types compared against the new infer keyword, which introduces a new type variable. TypeScript 2.8 also offers new type aliases that use conditional types.

Other new features in TypeScript 2.8 include:

1 2 Page 1
Page 1 of 2