We are assuming that if you are reading this article, you likely have more than a partial interest in TypeScript. And you are more than aware that it is a rigid superset of JavaScript that provides a class-based object-oriented programming aspect to the language. Developers absolutely love this programming language, and with every new release there are new features that developers find amazingly useful. Coupled with its relative ease to grasp and code in, we all can see why TypeScript is snapping at the heels of JavaScript as the predominant language used in the web.
But what are the key new features developers can expect from TypeScript releases this year? The latest TypeScript version now available is 4.1, with 4.2 currently in the beta testing phase.
New features in TypeScript v4.1
TypeScript 4.1 has vastly improved the programming language's functionality and speed while giving developers greater control.
New Template Type
This version includes the new template literal type, which acts as a building block for compiling other string types and enables developers to model functions and APIs that expect a set of specific strings.
String literal types could be used as building blocks: building other string literal types, and that is why TypeScript 4.1 gives the template literal string type. It has the same syntax as template literal strings in JavaScript, but is used in type positions.
When used with concrete literal types, it produces a new string literal type by concatenating the contents.
type World = "world";
type Greeting = `hello ${World}`;
// ^ = type Greeting = "hello world"
Re-Map keys - 'as' Clause
Developers can now re-map keys in mapped types by using 'as' as a clause. Previously, mapped types could only produce new object types with already provided keys to use only. Developers can now create new keys or filter out keys based on the input.
Other new features:
- Additional new features include improved support for the JSDoc tag and support of React 17's jsx and jsxs factory functions. Developers can now use functionality like the go-to-definition in a dotted name following the tag.
- There is also a new flag called --noUncheckedIndexedAccess for helping devs catch out-of-bounds errors.
TypeScript v4.2 - What's in store for the next release?
Rest Elements Changes in Tuple Types
In TypeScript 4.2, the rest elements have explicitly been expanded in how they can be used. In prior versions, TypeScript only allowed '...rest' elements at the very last position of a tuple type. Rest elements can now occur anywhere within a tuple – with only minor restrictions.
let foo: [...string[], number];
foo = [123];
foo = ["hello", 123];
foo = ["hello!", "hello!", "hello!", 123];
let bar: [boolean, ...string[], boolean];
bar = [true, false];
bar = [true, "some text", false];
bar = [true, "some", "separated", "text", false];
Smarter Type Inclusion
In TypeScript 4.2, the internal engine becomes somewhat smarter. Developers tracked how types were constructed by keeping around parts of their original structures before they normalized them. Developer's also kept track of type aliases to instances of other aliases.
Strict Internal Checking for the 'in' Operator
In JavaScript, placing a non-object type on the right side of the 'in' operator will result in a runtime error. In TypeScript 4.2, this will be detected at design-time.
"foo" in 42
// ~~
// error! The right-hand side of an 'in' expression must not be a primitive.
abstract Construct Signatures
TypeScript permits developers to mark a class as abstract. TypeScript will know that the class is only meant to be extended from and that certain members need to be filled in by any subclass to actually create an instance.
In TypeScript 4.2, developer's will be allowed to specify an abstract modifier on constructor signatures. Adding the abstract modifier to a construct signature signals that you can pass in abstract constructors. It doesn't stop you from passing in other classes/constructor functions that are "concrete" – it just signals that there's no intent to run the constructor directly, so it's safe to pass in either class type.
interface HasArea {
getArea(): number;
}
// Works!
let Ctor: abstract new () => HasArea = Shape;
// ^^^^^^^^
Adding the abstract modifier to a construct signature signals that you can pass in abstract constructors.
Conclusion
So what is in store for TypeScript after v4.2 is released? At the time of writing this article, the release date for TypeScript v4.2 is currently the 23rd of February, 2021, and there are no further planned releases (to date) after v4.2 - but we know that there will be, and it is merely a question of when.