TypeScript 2.4: Dynamic imports and weak types

By on June 28, 2017 12:00 pm

The TypeScript 2.4 release might be a minor update in terms of not requiring substantial changes within our open source work and customer projects, but it provides some major benefits that we are already leveraging throughout the Dojo 2 codebase.

Dynamic import()

The headline feature for TypeScript 2.4 is support for the ES.Next dynamic import() expressions that emit down to a require and will enable us to replace the existing @dojo/core/load module that is currently used to lazily load modules and mark modules for code splitting, and remove a significant amount of code and complexity from our @dojo/cli-build-webpack cli command package. The ability to dynamically import() is one of the key features that has been missing from ES Modules when compared to AMD.

Weak types and generics

Significant time has also been put into improving type checking for generics and stricter checks on weak types. A weak type is essentially a type that contains only optional properties. Basically because TypeScript equated these weak types to the any type, which could lead to very dangerous code, where TypeScript wouldn’t error with the following code:

function foo(weak: { foo?: string }) { }

foo('anything'); // <- supposed to be an object!

Weak type detection has helped provide improvements across all the Dojo 2 packages simplifying and correcting existing typing behaviors and definitions. Including in one case highlighting an existing bug in @dojo/core/request that we were able to fix.

String enums

Last but certainly not least, a feature that we have long awaited for Dojo 2 is string enums. We will soon be working to add these to the Dojo 2 codebase, and believe that these will make maintenance and readability of our code better in the long term. Expect to see string enums in the Dojo 2 code base very soon!

Thanks!

Thanks again to the TypeScript team for another round of useful improvements that help not only Dojo 2 but all TypeScript-based applications and frameworks.

Learning more

Read the TypeScript 2.4 blog for complete details of everything in the TypeScript 2.4 release.

If you’re based in or near London, join us for an immersive introduction to TypeScript for the Enterprise Developer on August 31st. Or start by reading our TypeScript the Definitive Guide. Or if you’re looking for hands-on assistance with your next modern JavaScript or TypeScript project, let us know how we can help!

Comments