Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ


Choose your language

InfoQ Homepage News TypeScript 3.2 Improves Metaprogramming Support and Adds BigInt

TypeScript 3.2 Improves Metaprogramming Support and Adds BigInt

This item in japanese

The TypeScript team has released version 3.2 of TypeScript, improving support for various metaprogramming patterns, and Object spread and rest on generic types. The release also includes BigInt support for environments which support this stage 3 ECMAScript proposal.

TypeScript 3.2 adds stricter type checking for bind, call, and apply, one of the critical pieces towards full support for variadic kinds. TypeScript program manager Daniel Rosenwasser explains the importance of these additional type checks:

In JavaScript, bind, call, and apply are methods on functions that allow us to do things like bind this and partially apply arguments, call functions with a different value for this, and call functions with an array for their arguments.

TypeScript could not previously strictly check these functions and each of bind, call, and apply were typed to accept any number of arguments and return the any type. Two earlier features support the necessary abstractions to type bind, call, and apply accurately:

  • this parameter types (TypeScript 2.0)
  • Modeling parameter lists with tuple types (TypeScript 3.0)

The TypeScript 3.2 release introduces a new flag, strictBindCallApply, which adds two new global types for declaring stricter versions of signatures for bind, call, and apply:

  • CallableFunction - methods on callable objects
  • NewableFunction - methods on constructable but not callable objects

The TypeScript team expects this feature to help catch bugs when using sophisticated metaprogramming, or simple patterns like binding methods within class instances.

Object spread and rest are two widely adopted features from ES2015 which are supported by most of TypeScript, but until this release was not available for generic types.

In the case of spread, TypeScript had no way of expressing the return type from the generic type, so there was no mechanism to express two unknown types getting spread into a new one. TypeScript 3.2 now allows object spreads for generics, and models this functionality using intersection types.

For object rest, instead of creating a new object with some extra/overridden properties, rest creates a new object lacking some specified properties. However, after iterating through many ideas, the TypeScript team realized that the existing Pick and Exclude helper types provide the ability to perform an object rest on a generic type.

BigInt support is near complete and is expected to be part of the ES2019 standard. BigInts allow the handling of arbitrarily large integers. TypeScript 3.2 adds type-checking for BigInts and emits BigInt literals for environments with BigInt support via the TypeScript esnext target.

Because BigInts introduce different behavior for mathematical operators, the TypeScript team has no immediate plans to provide down-leveling support for environments without BigInt support. Today, this means that BigInt support only works for Node.js 11+, Chrome 67+, and browsers based on similar versions of Chromium. Firefox, Safari, and Edge all report working on BigInt implementations.

Other improvements to TypeScript 3.2 include:

  • Allowing non-unit types in union discriminants
  • Supporting Object.defineProperty property assignments in JavaScript
  • Supporting the printing of an implied configuration object to the console with --showConfig
  • Improving the formatting and indentation for lists and chained calls
  • Scaffolding local @types packages with dts-gen
  • Adding intermediate unknown type assertions
  • Adding missing new keyword

Review the TypeScript roadmap for full details of these and other changes in the TypeScript 3.2 release.

Work has already begun on TypeScript 3.3, with Partial type argument inference being the first planned feature. This change would allow _ to appear in type argument lists as a placeholder for locations for type inference to occur, allowing users to override a variable in a list of defaults without explicitly providing the rest, or allowing inference of a type variable from another one.

The other major feature on the TypeScript roadmap which may appear for TypeScript 3.3 is alignment with recent changes to the ES decorators proposal.

TypeScript is open source software available under the Apache 2 license. Contributions and feedback are encouraged via the TypeScript GitHub project and should follow the TypeScript contribution guidelines and Microsoft open source code of conduct.

Rate this Article