News

TypeScript 2.8 Officially Released: Hello Conditional Types

Less than two weeks after publishing the TypeScript 2.8 Release Candidate, Microsoft has officially launched it into general availability.

TypeScript is an open source language backed by Microsoft that provides optional static typing while remaining a strict syntactical superset of JavaScript.

The big new feature in TypeScript 2.8 is the support for conditional types, which borrows from JavaScript's familiar conditional -- or ternary operator, "?" -- that returns a different expression depending on whether a preceding Boolean conditional expression evaluates to true or false, acting as shorthand for the if ... then ... construct.

In TypeScript 2.8 the new functionality involving the ternary operator opens up all kinds of possibilities, Microsoft's Daniel Rosenwasser explained:

Conditional types are a new construct in TypeScript that allow us to choose types based on other types. They take the form
A extends B ? C : D
where A, B, C and D are all types. You should read that as "when the type A is assignable to B, then this type is C; otherwise, it's D." If you've used conditional syntax in JavaScript, this will feel familiar to you.

Although that's the simplest example, the new feature can be put to many more complicated use cases, including working with generics.

"While conditional types can be a little intimidating at first, we believe they'll bring a ton of flexibility for moments when you need to push the type system a little further to get accurate types," Rosenwasser said.

The new conditional type functionality stems from a pull request for Microsoft's open source programming language announced by Technical Fellow Anders Hejlsberg on Jan. 20. That PR goes into all of the nitty-gritty details of the new feature, also explained further in Rosenwasser's post.

Speaking of which, Rosenwasser also summarized the following features in TypeScript 2.8:

"We hope that TypeScript 2.8 pushes the envelope further to provide a type system that can truly represent the nature of JavaScript as a language," Rosenwasser concluded. "With that, we believe we can provide you with an experience that continues to make you more productive and happier as you code."

TypeScript has apparently already been making its users happier, as it was named a popular programming language in Stack Overflow's big developer survey and the most recent RedMonk language popularity report -- though some developers have debated its perceived "bloat."

More information can be found in the list of breaking changes and the language's roadmap, which shows what's coming next, including generic JSX elements.

About the Author

David Ramel is an editor and writer at Converge 360.

comments powered by Disqus

Featured

  • Compare New GitHub Copilot Free Plan for Visual Studio/VS Code to Paid Plans

    The free plan restricts the number of completions, chat requests and access to AI models, being suitable for occasional users and small projects.

  • Diving Deep into .NET MAUI

    Ever since someone figured out that fiddling bits results in source code, developers have sought one codebase for all types of apps on all platforms, with Microsoft's latest attempt to further that effort being .NET MAUI.

  • Copilot AI Boosts Abound in New VS Code v1.96

    Microsoft improved on its new "Copilot Edit" functionality in the latest release of Visual Studio Code, v1.96, its open-source based code editor that has become the most popular in the world according to many surveys.

  • AdaBoost Regression Using C#

    Dr. James McCaffrey from Microsoft Research presents a complete end-to-end demonstration of the AdaBoost.R2 algorithm for regression problems (where the goal is to predict a single numeric value). The implementation follows the original source research paper closely, so you can use it as a guide for customization for specific scenarios.

  • Versioning and Documenting ASP.NET Core Services

    Building an API with ASP.NET Core is only half the job. If your API is going to live more than one release cycle, you're going to need to version it. If you have other people building clients for it, you're going to need to document it.

Subscribe on YouTube