TypeScript Adds Conditional Types
TypeScript Adds Conditional Types
Written by Kay Ewbank   
Tuesday, 03 April 2018

Support for conditional types has been added to TypeScript 2.8, the language that adds optional static types to JavaScript.

TypeScript was developed by Microsoft, and is a superset of JavaScript that builds on the ECMAScript standard and includes support for static types. Your TypeScript code then gets transformed into clean, runnable JavaScript. TypeScript includes editor support for Visual Studio 2015 and 2017, Sublime Text 3 and from the next release, Visual Studio Code.

The headline improvement to the new release is support for conditional types. These are a new construct in TypeScript that let you choose types based on other types. According to Microsoft's Daniel Rosenwasser:

"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."

The conditional syntax obviously mimics that in JavaScript. Conditional types come into their own when used with generics, where they can avoid having to create multiple overloads to handle each case where the type is known and also when it's not known. The use of a type alias also enables the reuse of the logic.

The second improvement is the addition of a declaration-only emit. This gives you a way to emit only the declaration files, without also generating JavaScript files or sourcemap files. One reason you might want to do this is if you're using an alternate compiler for TypeScript such as Babel 7.

The next addition lets you set your JSX factory on a file-by-file basis, so you can use a mix of libraries in the same application.  JSX tags are usually rewritten to React.createElement,but that can be changed if you're using libraries that have a React-like factory API, such as Preact, Stencil, Inferno or Cycle. Until now, this change had to be done globally, so you couldn't mix the libraries and use JSX for both.

Anther improvement in this release is more granular control on mapped type modifiers so you can rmove modifiers in mapped types, as well as adding modifiers with the + operator.

 

typescriptlogo

More Information

TypeScript on NuGet

TypeScript On GitHub

Related Articles

TypeScript 2.7 Improves Type Inference

TypeScript 2.5 Adds Optional Catch Binding

TypeScript 2.4 Adds Dynamic Import Expressions  

TypeScript 2.3 Released

TypeScript 2.2 Adds More Code Actions

 

 

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on, Twitter, FacebookGoogle+ or Linkedin.

Banner


All A Flutter - Google's Cross Platform App Framework Ready To Go
22/06/2018

Google's Flutter is about to be released as a Version 1 product, ready for you to use. The question is should you be interested in a way to create almost native apps that run on iOS or Android? I supp [ ... ]



DevTube - A Curated Collection of Videos
26/06/2018

There's a new resource for developers. Dev.Tube is where you will find YouTube videos from conferences collected by developers for developers. The website already includes 10,000 videos - of whic [ ... ]


More News

justjsquare

 



 

Comments




or email your comment to: comments@i-programmer.info


TypeScript 2.7 Improves Type Inference
 
 

   
Banner
Banner
RSS feed of news items only
I Programmer News
Copyright © 2018 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.