TypeScript 2.7 has been released with several major features and bug fixes. Some of the highlights include support for assignment checks on class properties, fixed length tuples, and improved type inference for object literals. Overall the changes in this release make improvements to the type system, ES2015+ features, and the overall TypeScript developer experience.
The TypeScript 2.7 release introduces a few breaking changes, which developers should be aware of when upgrading a code base to leverage TypeScript 2.7.
When asked about the current release plans for TypeScript, program manager Daniel Rosenwasser said:
I think in the broadest sense, we’re focused on ease of adoption, and making sure our existing users are still getting new value from investing in TypeScript. The former is usually about eliminating friction in starting out, and the latter is often about expressivity or more safety.
For example, existing TypeScript users were requesting stricter checks around class initialization. Following the strict function types check added in TypeScript 2.6, 2.7 adds strict property initialization checks in classes with the --strictPropertyInitialization
flag. This flag checks that each instance property of a class gets initialized in the constructor body, or by a property initializer. Developers leveraging the --strict
flag will get this additional check automatically with their source code when upgrading to 2.7. Leveraging the strict flag in TypeScript is recommended as it helps developers find and eliminate additional types of errors or unexpected behavior.
Definite assignment assertions are now possible, to instruct TypeScript’s control flow analyzer to always consider a variable definitely assigned, even when the TypeScript analyzer is unable to prove the assignment.
TypeScript architect Anders Hejlsberg discussed the improved type inference for object literals:
improves type inference for multiple object literals occurring in the same context. When multiple object literal types contribute to a union type, we now normalize the object literal types such that all properties are present in each constituent of the union type.Previously we inferred type {} for obj and the second line subsequently caused an error because obj would appear to have no properties. That obviously wasn't ideal.const obj = test ? { text: "hello" } : {}; // { text: string } | { text?: undefined } const s = obj.text; // string | undefined
Additional language-level improvements include improving the handling of structurally identical classes in union types and instanceof
expressions and better type guards inferred from the in
operator.
TypeScript 2.7 also refines support with ES2015+ symbols. Properties can now be named with const
-declared symbols, allowing the use of an Identifier
or PropertyAccessExpression
as part of a computed property name in an interface, class, or type literal.
Interoperability with CommonJS modules has improved with support for import d from "cjs"
with the --esModuleInterop
flag. Internally, TypeScript now leverages ES modules over CJS modules. Rosenwasser explained to InfoQ the motivation behind this feature:
We also knew there was a friction in module interoperability strategies which made it harder for Babel users to dive into TypeScript. That’s a barrier to entry for new TypeScript users, which is why we made the
--esModuleInterop
flag a priority for this release. Of course, there’s also updates to the ECMAScript standard, which we are involved in, and are very mindful of as we grow the language.
The stage 3 numeric separators proposal, expected to be part of ES2018, is now included with TypeScript, helping TypeScript stay current with annual updates to the JavaScript language.
A few changes in TypeScript 2.7 target the developer experience. Development tools such as webpack and Gulp will benefit from the new incremental builder compiler API. This change is expected to make it easier for tools to more easily update just the resources impacted by code changes rather than needing to rebuild an entire project on each incremental update. Additionally, IDEs are provided with an updated TypeScript Language Services API to allow developers to automatically fix all identified issues in a file.
TypeScript 2.7 is available via npm with the npm install -g typescript
command, or via GitHub. Efforts on TypeScript 2.8 are already underway, with a release anticipated in late March.