Additionally, generators just assumed the type of was always any.

  1. function* bar() {
  2. let x: { hello(): void } = yield;
  3. x.hello();
  4. }
  5. let iter = bar();
  6. iter.next();
  7. iter.next(123); // oops! runtime error!

In TypeScript 3.6, the checker now knows that the correct type for curr.value should be string in our first example, and will correctly error on our call to next() in our last example.This is thanks to some changes in the Iterator and IteratorResult type declarations to include a few new type parameters, and to a new type that TypeScript uses to represent generators called the Generator type.

The Iterator type now allows users to specify the yielded type, the returned type, and the type that next can accept.

  1. interface Iterator<T, TReturn = any, TNext = undefined> {
  2. // Takes either 0 or 1 arguments - doesn't accept 'undefined'
  3. next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
  4. return?(value?: TReturn): IteratorResult<T, TReturn>;
  5. throw?(e?: any): IteratorResult<T, TReturn>;
  6. }

Building on that work, the new Generator type is an Iterator that always has both the return and throw methods present, and is also iterable.

  1. interface Generator<T = unknown, TReturn = any, TNext = unknown>
  2. extends Iterator<T, TReturn, TNext> {
  3. next(...args: [] | [TNext]): IteratorResult<T, TReturn>;
  4. return(value: TReturn): IteratorResult<T, TReturn>;
  5. throw(e: any): IteratorResult<T, TReturn>;
  6. [Symbol.iterator](): Generator<T, TReturn, TNext>;
  7. }

To allow differentiation between returned values and yielded values, TypeScript 3.6 converts the IteratorResult type to a discriminated union type:

  1. type IteratorResult<T, TReturn = any> = IteratorYieldResult<T> | IteratorReturnResult<TReturn>;
  2. interface IteratorYieldResult<TYield> {
  3. done?: false;
  4. value: TYield;
  5. }
  6. done: true;
  7. value: TReturn;
  8. }

In short, what this means is that you’ll be able to appropriately narrow down values from iterators when dealing with them directly.

To correctly represent the types that can be passed in to a generator from calls to next(), TypeScript 3.6 also infers certain uses of yield within the body of a generator function.

If you’d prefer to be explicit, you can also enforce the type of values that can be returned, yielded, and evaluated from yield expressions using an explicit return type.Below, next() can only be called with booleans, and depending on the value of done, value is either a string or a number.

  1. /**
  2. * - yields numbers
  3. * - returns strings
  4. * - can be passed in booleans
  5. function* counter(): Generator<number, string, boolean> {
  6. let i = 0;
  7. while (true) {
  8. if (yield i++) {
  9. break;
  10. }
  11. }
  12. return "done!";
  13. }
  14. var iter = counter();
  15. var curr = iter.next()
  16. while (!curr.done) {
  17. console.log(curr.value);
  18. curr = iter.next(curr.value === 5)
  19. }
  20. console.log(curr.value.toUpperCase());
  21. // prints:
  22. //
  23. // 0
  24. // 1
  25. // 2
  26. // 3
  27. // 4
  28. // 5
  29. // DONE!

For more details on the change, .

More Accurate Array Spread

In pre-ES2015 targets, the most faithful emit for constructs like for/of loops and array spreads can be a bit heavy.For this reason, TypeScript uses a simpler emit by default that only supports array types, and supports iterating on other types using the —downlevelIteration flag.The looser default without —downlevelIteration works fairly well; however, there were some common cases where the transformation of array spreads had observable differences.For example, the following array containing a spread

  1. [...Array(5)]

can be rewritten as the following array literal

  1. [undefined, undefined, undefined, undefined, undefined]

However, TypeScript would instead transform the original code into this code:

  1. Array(5).slice();

which is slightly different.Array(5) produces an array with a length of 5, but with no defined property slots.

For more information, .

Improved UX Around Promises

TypeScript 3.6 introduces some improvements for when Promises are mis-handled.

For example, it’s often very common to forget to .then() or await the contents of a Promise before passing it to another function.TypeScript’s error messages are now specialized, and inform the user that perhaps they should consider using the await keyword.

It’s also common to try to access a method before await-ing or .then()-ing a Promise.This is another example, among many others, where we’re able to do better.

  1. fetch("https://reddit.com/r/aww.json")
  2. .json()
  3. // ~~~~
  4. // Property 'json' does not exist on type 'Promise<Response>'.
  5. //
  6. // Did you forget to use 'await'?
  7. }

For more details, , as well as the pull requests that link back to it.

TypeScript 3.6 contains better support for Unicode characters in identifiers when emitting to ES2015 and later targets.

  1. const 𝓱𝓮𝓵𝓵𝓸 = "world"; // previously disallowed, now allowed in '--target es2015'

import.meta Support in SystemJS

TypeScript 3.6 supports transforming import.meta to context.meta when your module target is set to .

  1. // This module:
  2. console.log(import.meta.url)
  3. // gets turned into the following:
  4. System.register([], function (exports, context) {
  5. return {
  6. setters: [],
  7. execute: function () {
  8. console.log(context.meta.url);
  9. }
  10. };
  11. });

get and set Accessors Are Allowed in Ambient Contexts

In previous versions of TypeScript, the language didn’t allow get and set accessors in ambient contexts (like in declare-d classes, or in .d.ts files in general).The rationale was that accessors weren’t distinct from properties as far as writing and reading to these properties;however, because ECMAScript’s class fields proposal may have differing behavior from in existing versions of TypeScript, we realized we needed a way to communicate this different behavior to provide appropriate errors in subclasses.

As a result, users can write getters and setters in ambient contexts in TypeScript 3.6.

  1. declare class Foo {
  2. // Allowed in 3.6+.
  3. get x(): number;
  4. set x(val: number): void;
  5. }

In TypeScript 3.7, the compiler itself will take advantage of this feature so that generated .d.ts files will also emit get/set accessors.

In previous versions of TypeScript, it was an error to merge classes and functions under any circumstances.Now, ambient classes and functions (classes/functions with the declare modifier, or in .d.ts files) can merge.This means that now you can write the following:

instead of needing to use

  1. export interface Point2D {
  2. x: number;
  3. y: number;
  4. }
  5. export declare var Point2D: {
  6. (x: number, y: number): Point2D;
  7. new (x: number, y: number): Point2D;
  8. }

One advantage of this is that the callable constructor pattern can be easily expressed while also allowing namespaces to merge with these declarations (since var declarations can’t merge with namespaces).

For more details, .

APIs to Support —build and —incremental

TypeScript 3.0 introduced support for referencing other and building them incrementally using the —build flag.Additionally, TypeScript 3.4 introduced the —incremental flag for saving information about previous compilations to only rebuild certain files.These flags were incredibly useful for structuring projects more flexibly and speeding builds up.Unfortunately, using these flags didn’t work with 3rd party build tools like Gulp and Webpack.TypeScript 3.6 now exposes two sets of APIs to operate on project references and incremental program building.

For creating —incremental builds, users can leverage the createIncrementalProgram and createIncrementalCompilerHost APIs.Users can also re-hydrate old program instances from .tsbuildinfo files generated by this API using the newly exposed readBuilderProgram function, which is only meant to be used as for creating new programs (i.e. you can’t modify the returned instance - it’s only meant to be used for the oldProgram parameter in other create*Program functions).

For leveraging project references, a new createSolutionBuilder function has been exposed, which returns an instance of the new type SolutionBuilder.

For more details on these APIs, you can .

Semicolon-Aware Code Edits

Editors like Visual Studio and Visual Studio Code can automatically apply quick fixes, refactorings, and other transformations like automatically importing values from other modules.These transformations are powered by TypeScript, and older versions of TypeScript unconditionally added semicolons to the end of every statement; unfortunately, this disagreed with many users’ style guidelines, and many users were displeased with the editor inserting semicolons.

TypeScript is now smart enough to detect whether your file uses semicolons when applying these sorts of edits.If your file generally lacks semicolons, TypeScript won’t add one.

For more details, .

JavaScript has a lot of different module syntaxes or conventions: the one in the ECMAScript standard, the one Node already supports (CommonJS), AMD, System.js, and more!For the most part, TypeScript would default to auto-importing using ECMAScript module syntax, which was often inappropriate in certain TypeScript projects with different compiler settings, or in Node projects with plain JavaScript and require calls.

TypeScript 3.6 is now a bit smarter about looking at your existing imports before deciding on how to auto-import other modules.You can see more details in the original pull request here.

New TypeScript Playground

The TypeScript playground has received a much-needed refresh with handy new functionality!The new playground is largely a fork of Artem Tyurin’s which community members have been using more and more.We owe Artem a big thanks for helping out here!

The new playground now supports many new options including:

  • The target option (allowing users to switch out of es5 to es3, es2015, esnext, etc.)
  • All the strictness flags (including just strict)
  • Support for plain JavaScript files (using allowJS and optionally )

These options also persist when sharing links to playground samples, allowing users to more reliably share examples without having to tell the recipient “oh, don’t forget to turn on the noImplicitAny option!”.

As we improve the playground and the website, we welcome feedback and pull requests on GitHub!