Member-only story
What’s New in TypeScript 3.9?
An overview of everything that just went live
TypeScript just released its second release for the year on May 12. It’s version 3.9, which is now the stable version. In this article, I’m going to point out some of the new and exciting features of TypeScript 3.9.
@ts-expect-error
Let’s take an example where we define a function that takes two strings as parameters.
printName(firstName: string, lastName: string) {
console.log(firstName);
console.log(lastName);
assert(typeof firstName === "string");
assert(typeof lastName === "string");
}
Usually, TypeScript users will get a helpful red squiggle and an error message when they misuse this function, and JavaScript users will get an assertion error. But what will happen if we write a unit test to check this functionality?
expect(() => {
printName(1234, 5678);
}).toThrow();
If our test is written in TS, it’ll throw an error like this:
printName(1234, 5678);
// ~~~
// error: Type ‘number’ is not assignable to type ‘string’.
As a solution for this, version 3.9 of TypeScript brings a new feature: // @ts-expect-error
comments. If we put this comment before a code line as a prefix…