iTranslated by AI
The "Weakest Update" in TypeScript 4.2 and 4.3
TypeScript 4.3 RC was recently released, and the official launch is approaching. In this article, I will introduce what could be called the "weakest update" that occurred from TypeScript 4.2 to 4.3, and the shift in best practices that followed.
Addition of abstract modifier to construct signatures in TypeScript 4.2
A construct signature is a concept in the type system that represents a constructor—specifically, a "function that can be new-ed." Starting from TypeScript 4.2, it is now possible to add the abstract modifier to construct signatures.
// A function object with a normal construct signature
declare const foo: new (arg: number) => unknown;
const obj1: unknown = new foo(123);
// A function object with an abstract construct signature
declare const bar: abstract new (arg: number) => unknown;
// This results in an error
// Error: Cannot create an instance of an abstract class.
const obj2: unknown = new bar(123);
TypeScript has long had the concept of abstract classes and the functionality to prevent new-ing them. However, this "abstractness" was previously a flag given only to specific class types (types obtained solely through the abstract class syntax).
// Declaration of an abstract class
abstract class Abs {}
// Error: Cannot create an instance of an abstract class.
const obj1: unknown = new Abs();
Previously, abstract classes could not be passed to "functions that receive a class."
// Error: Argument of type 'typeof Abs' is not assignable to parameter of type 'new () => unknown'.
// Cannot assign an abstract constructor type to a non-abstract constructor type.
useClass(Abs);
function useClass(cls: new () => unknown) {
new cls();
}
In other words, there was previously no way to declare a function that could "receive even an abstract class." The abstract construct signature in TypeScript 4.2 made this possible.
// Now you can pass an abstract class!
useClass(Abs);
function useClass(cls: abstract new () => unknown) {}
With this, the concept of an abstract class has gained first-class status within the type system. This is the change that occurred in TypeScript 4.2.
Standard Library Changes in TypeScript 4.3
In TypeScript 4.3, abstract construct signatures were incorporated into the standard library.
- type InstanceType<T extends new (...args: any) => any> = T extends new (...args: any) => infer R ? R : any;
+ type InstanceType<T extends abstract new (...args: any) => any> = T extends abstract new (...args: any) => infer R ? R : any;
This made it possible to do things that were previously impossible, such as passing an abstract class (its type) to the InstanceType type.
The "Weakest Update" caused by abstract construct signatures
The standard library update described above represents the "weakest update" brought about by abstract construct signatures. In this article, "weakest" refers to the strength or weakness of a type. That is, a strong type is a type with many restrictions, while a weak type is one with fewer restrictions. The weakest type in TypeScript is the unknown type, which accepts any value without any restrictions.
When defining a function, it is considered desirable to make the types of the arguments as weak as possible. This is because it results in a more versatile and reusable function. In the case of construct signatures, for a "function that receives a constructor but does not new it internally," the weakest choice previously was a type like new () => unknown. However, with the introduction of the abstract new () => unknown type, the "weakest" has been updated. A specific example of such a function is one used solely to extends a given constructor, as mentioned in the official TypeScript release notes (the following example uses many as casts because TypeScript's inference isn't quite there yet, but please focus on the interface).
// The previous weakest
function addDefaultParam<T extends object>(cls: new (str: string) => T) {
class A extends (cls as new (str: string) => object) {
constructor() {
super("pikachu");
}
}
return A as new () => T;
}
// The new weakest
function addDefaultParam2<T extends object>(
cls: abstract new (str: string) => T
) {
class A extends (cls as abstract new (str: string) => object) {
constructor() {
super("pikachu");
}
}
return A as new () => T;
}
Also, examples like InstanceType from the TypeScript 4.3 release notes can be seen as cases where the "weakest update" occurred within the type system.
The update of the weakest is also an update of common knowledge. Previously, there was a common understanding that "if you want to make a function that receives a class (without being new-ed inside the function) more versatile, accepting a construct signature is the best practice." However, with the addition of the feature in TypeScript 4.2, that common knowledge has been updated to: "if you want to make a function that receives a class (without being new-ed inside the function) more versatile, accepting an abstract construct signature is the best practice." The changes in the standard library in TypeScript 4.3 are a result of adapting to this new common knowledge.
In this way, the addition of features in TypeScript can lead to updates in common knowledge. For all you TypeScript users out there, be sure not to miss these changes so you can write code based on the latest best practices.
Other Examples of the "Weakest Update"
There are other examples in the history of TypeScript where a "weakest update" occurred. A prominent one is the readonly array type. The ReadonlyArray<T> type was added in TypeScript 2.0, and the readonly T[] syntax was added in TypeScript 3.4. Previously, the best practice for the argument type of a "function that receives an array but does not perform destructive changes" was the T[] type, but with the addition of the readonly array type, it changed to the readonly T[] type. Nowadays, it is no exaggeration to say that intentionally using T[] instead of readonly T[] for an argument type is a declaration of intent that "this function will destructively modify the given array." This is another example where the addition of a feature in TypeScript changed common knowledge, and as a result, even the intent read from the program has changed.
Summary
In TypeScript, best practices can shift in the form of a "weakest update" as the type system evolves. From TypeScript 4.2 onwards, it has become the new best practice to use an abstract construct signature type for the arguments of functions that receive a class but do not new it.
Discussion