TypeScript 2.3 is the latest version in the quarterly release cycle from the TypeScript team. This release contains several useful additions to TypeScript which we have already started to leverage within some of our current projects. Let’s take a closer look!

Default type arguments

A seemingly simple change is the ability to provide default values for Generic arguments. The benefit for engineers is that it changes type signatures that might be particularly complex. Here’s an example in which we define the API structure for creating widget structures in Dojo 2, from:

export function w<P extends WidgetProperties>(widgetConstructor: WidgetBaseConstructor<P> | string, properties: P, children?: DNode[]): WNode;
export function w<P extends WidgetProperties>(widgetConstructor: WidgetBaseConstructor<P> | string, children: DNode[]): WNode;
export function w<P extends WidgetProperties>(widgetConstructor: WidgetBaseConstructor<P> | string): WNode;
export function w<P extends WidgetProperties>(widgetConstructor: WidgetBaseConstructor<P> | string, propertiesOrChildren: P | DNode[] = <P> {}, children: DNode[] = []): WNode {
		let properties: P;

	if (Array.isArray(propertiesOrChildren)) {
		children = propertiesOrChildren;
		properties = <P> {};
	}
	else {
		properties = propertiesOrChildren;
	}

to a more concise, clear and maintainable version of the same concept:

export function w<P extends WidgetProperties = WidgetProperties, C extends WidgetProperties = WidgetProperties>(widgetConstructor: WidgetBaseConstructor<P, C> | string, properties: WidgetProperties & P, children: DNode<C>[] = []): WNode<P, C> {

Once we had landed these modifications to support Generic default types, we were able to further refine our approach:

export function w<W extends WidgetBaseInterface>(widgetConstructor: Constructor<W> | string, properties: W['properties'], children: W['children'] = []): WNode<W> {

These improvements are simple to implement and are already available in Dojo 2. Within our APIs, there are often default values for information that the API doesn’t actually care about. Default type arguments allow us to stop thinking about unused generics, thus simplifying our type definitions. Sexy, right?!

Type checking in JavaScript files

In one of our previous SitePen podcasts, we discussed at length the negative viewpoint that TypeScript was a system that required typing everything up front. TypeScript 2.3 makes inroads to addressing this complaint and it’s now possible to run TypeScript against JS files using two options:

  • <strong>checkJs</strong> – type check all JS files in your project
  • <strong>@ts-check</strong> – a per file comment to check a subset of JS files

Other additions to TS 2.3

Beyond bug fixes and minor improvements, other notable additions to TypeScript 2.3 include:

  • Language server plugin support: support alternative language syntax, such as support for Angular templates, GraphQL, Vue.js, and more
  • Generator and async generator down-emitting for ES3 and ES5
  • Simplifications to configuration and help

Summary

The TypeScript team continues to make progress improving the workflow and capabilities of developing with TypeScript. We’ve already updated our work to leverage default type arguments, and we look forward to exploring the language server plugin support in the near future.