Constant-named properties

TypeScript 2.7 adds support for declaring const-named properties on types including ECMAScript symbols.

Example
  1. // Lib
  2. export const SERIALIZE = Symbol("serialize-method-key");
  3. export interface Serializable {
  4. [SERIALIZE](obj: {}): string;
  5. }
  1. // consumer
  2. import { SERIALIZE, Serializable } from "lib";
  3. class JSONSerializableItem implements Serializable {
  4. [SERIALIZE](obj: {}) {
  5. return JSON.stringify(obj);
  6. }
  7. }

This also applies to numeric and string literals.

Example
  1. const Foo = "Foo";
  2. const Bar = "Bar";
  3. let x = {
  4. [Foo]: 100,
  5. [Bar]: "hello",
  6. };
  7. let a = x[Foo]; // has type 'number'
  8. let b = x[Bar]; // has type 'string'

unique symbol

To enable treating symbols as unique literals a new type unique symbol is available.unique symbol is a subtype of symbol, and are produced only from calling Symbol() or Symbol.for(), or from explicit type annotations.The new type is only allowed on const declarations and readonly static properties, and in order to reference a specific unique symbol, you’ll have to use the typeof operator.Each reference to a unique symbol implies a completely unique identity that’s tied to a given declaration.

Example
  1. // Works
  2. declare const Foo: unique symbol;
  3. // Error! 'Bar' isn't a constant.
  4. let Bar: unique symbol = Symbol();
  5. // Works - refers to a unique symbol, but its identity is tied to 'Foo'.
  6. let Baz: typeof Foo = Foo;
  7. // Also works.
  8. class C {
  9. static readonly StaticSymbol: unique symbol = Symbol();
  10. }

Because each unique symbol has a completely separate identity, no two unique symbol types are assignable or comparable to each other.

Example
  1. const Foo = Symbol();
  2. const Bar = Symbol();
  3. // Error: can't compare two unique symbols.
  4. if (Foo === Bar) {
  5. // ...
  6. }

Strict Class Initialization

TypeScript 2.7 introduces a new flag called —strictPropertyInitialization.This flag performs checks to ensure that each instance property of a class gets initialized in the constructor body, or by a property initializer.For example

  1. class C {
  2. foo: number;
  3. bar = "hello";
  4. baz: boolean;
  5. // ~~~
  6. // Error! Property 'baz' has no initializer and is not definitely assigned in the
  7. // constructor.
  8. constructor() {
  9. this.foo = 42;
  10. }
  11. }

In the above, if we truly meant for baz to potentially be undefined, we should have declared it with the type boolean | undefined.

There are certain scenarios where properties can be initialized indirectly (perhaps by a helper method or dependency injection library), in which case you can use the new definite assignment assertion modifiers for your properties (discussed below).

  1. class C {
  2. foo!: number;
  3. // ^
  4. // Notice this '!' modifier.
  5. // This is the "definite assignment assertion"
  6. constructor() {
  7. this.initialize();
  8. }
  9. initialize() {
  10. this.foo = 0;
  11. }
  12. }

Keep in mind that —strictPropertyInitialization will be turned on along with other —strict mode flags, which can impact your project.You can set the strictPropertyInitialization setting to false in your tsconfig.json’s compilerOptions, or —strictPropertyInitialization false on the command line to turn off this checking.

Definite Assignment Assertions

The definite assignment assertion is a feature that allows a ! to be placed after instance property and variable declarations to relay to TypeScript that a variable is indeed assigned for all intents and purposes, even if TypeScript’s analyses cannot detect so.

Example
  1. let x: number;
  2. initialize();
  3. console.log(x + x);
  4. // ~ ~
  5. // Error! Variable 'x' is used before being assigned.
  6. function initialize() {
  7. x = 10;
  8. }

With definite assignment assertions, we can assert that x is really assigned by appending an ! to its declaration:

  1. // Notice the '!'
  2. let x!: number;
  3. initialize();
  4. // No error!
  5. console.log(x + x);
  6. function initialize() {
  7. x = 10;
  8. }

In a sense, the definite assignment assertion operator is the dual of the non-null assertion operator (in which expressions are post-fixed with a !), which we could also have used in the example.

  1. let x: number;
  2. initialize();
  3. // No error!
  4. console.log(x! + x!);
  5. function initialize() {
  6. x = 10;

In our example, we knew that all uses of x would be initialized so it makes more sense to use definite assignment assertions than non-null assertions.

Fixed Length Tuples

In TypeScript 2.6 and earlier, [number, string, string] was considered a subtype of [number, string].This was motivated by TypeScript’s structural nature; the first and second elements of a [number, string, string] are respectively subtypes of the first and second elements of [number, string].However, after examining real world usage of tuples, we noticed that most situations in which this was permitted was typically undesirable.

In TypeScript 2.7, tuples of different arities are no longer assignable to each other.Thanks to a pull request from Tycho Grouwstra, tuple types now encode their arity into the type of their respective length property.This is accomplished by leveraging numeric literal types, which now allow tuples to be distinct from tuples of different arities.

Conceptually, you might consider the type [number, string] to be equivalent to the following declaration of NumStrTuple:

  1. interface NumStrTuple extends Array<number | string> {
  2. 0: number;
  3. 1: string;
  4. length: 2; // using the numeric literal type '2'
  5. }

Note that this is a breaking change for some code.If you need to resort to the original behavior in which tuples only enforce a minimum length, you can use a similar declaration that does not explicitly define a length property, falling back to number.

  1. interface MinimumNumStrTuple extends Array<number | string> {
  2. 0: number;
  3. 1: string;
  4. }

Note that this does not imply tuples represent immutable arrays, but it is an implied convention.

Improved type inference for object literals

TypeScript 2.7 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.

Consider:

  1. const obj = test ? { text: "hello" } : {}; // { text: string } | { text?: undefined }
  2. const s = obj.text; // string | undefined

Previously type {} was inferred for obj and the second line subsequently caused an error because obj would appear to have no properties.That obviously wasn’t ideal.

Example
  1. // let obj: { a: number, b: number } |
  2. // { a: string, b?: undefined } |
  3. // { a?: undefined, b?: undefined }
  4. let obj = [{ a: 1, b: 2 }, { a: "abc" }, {}][0];
  5. obj.a; // string | number | undefined
  6. obj.b; // number | undefined

Multiple object literal type inferences for the same type parameter are similarly collapsed into a single normalized union type:

  1. declare function f<T>(...items: T[]): T;
  2. // let obj: { a: number, b: number } |
  3. // { a: string, b?: undefined } |
  4. // { a?: undefined, b?: undefined }
  5. let obj = f({ a: 1, b: 2 }, { a: "abc" }, {});
  6. obj.a; // string | number | undefined
  7. obj.b; // number | undefined

Improved handling of structurally identical classes and instanceof expressions

TypeScript 2.7 improves the handling of structurally identical classes in union types and instanceof expressions:

  • Structurally identical, but distinct, class types are now preserved in union types (instead of eliminating all but one).
  • Union type subtype reduction only removes a class type if it is a subclass of and derives from another class type in the union.
  • Type checking of the instanceof operator is now based on whether the type of the left operand derives from the type indicated by the right operand (as opposed to a structural subtype check).

This means that union types and instanceof properly distinguish between structurally identical classes.

Example
  1. class A {}
  2. class B extends A {}
  3. class C extends A {}
  4. class D extends A { c: string }
  5. class E extends D {}
  6. let x1 = !true ? new A() : new B(); // A
  7. let x2 = !true ? new B() : new C(); // B | C (previously B)
  8. let x3 = !true ? new C() : new D(); // C | D (previously C)
  9. let a1 = [new A(), new B(), new C(), new D(), new E()]; // A[]
  10. let a2 = [new B(), new C(), new D(), new E()]; // (B | C | D)[] (previously B[])
  11. function f1(x: B | C | D) {
  12. if (x instanceof B) {
  13. x; // B (previously B | D)
  14. }
  15. else if (x instanceof C) {
  16. x; // C
  17. }
  18. else {
  19. x; // D (previously never)
  20. }
  21. }

Type guards inferred from in operator

The in operator now acts as a narrowing expression for types.

For a n in x expression, where n is a string literal or string literal type and x is a union type, the “true” branch narrows to types which have an optional or required property n, and the “false” branch narrows to types which have an optional or missing property n.

Example
  1. interface A { a: number };
  2. interface B { b: string };
  3. function foo(x: A | B) {
  4. if ("a" in x) {
  5. return x.a;
  6. }
  7. return x.b;
  8. }

Support for import d from "cjs" from CommonJS modules with —esModuleInterop

TypeScript 2.7 updates CommonJS/AMD/UMD module emit to synthesize namespace records based on the presence of an __esModule indicator under —esModuleInterop.The change brings the generated output from TypeScript closer to that generated by Babel.

Previously CommonJS/AMD/UMD modules were treated in the same way as ES6 modules, resulting in a couple of problems. Namely:

  • TypeScript treats a namespace import (i.e. import * as foo from "foo") for a CommonJS/AMD/UMD module as equivalent to const foo = require("foo").Things are simple here, but they don’t work out if the primary object being imported is a primitive or a class or a function. ECMAScript spec stipulates that a namespace record is a plain object, and that a namespace import (foo in the example above) is not callable, though allowed by TypeScript
  • Similarly a default import (i.e. import d from "foo") for a CommonJS/AMD/UMD module as equivalent to const d = require("foo").default.Most of the CommonJS/AMD/UMD modules available today do not have a default export, making this import pattern practically unusable to import non-ES modules (i.e. CommonJS/AMD/UMD). For instance import fs from "fs" or import express from "express" are not allowed.

Under the new —esModuleInterop these two issues should be addressed:

  • A namespace import (i.e. import * as foo from "foo") is now correctly flagged as uncallable. Calling it will result in an error.
  • Default imports to CommonJS/AMD/UMD are now allowed (e.g. import fs from "fs"), and should work as expected.

Note: The new behavior is added under a flag to avoid unwarranted breaks to existing code bases. We highly recommend applying it both to new and existing projects.For existing projects, namespace imports (import * as express from "express"; express();) will need to be converted to default imports (import express from "express"; express();).

Example

With —esModuleInterop two new helpers are generated importStar and importDefault for import * and import default respectively.For instance input like:

  1. import * as foo from "foo";
  2. import b from "bar";

Will generate:

  1. "use strict";
  2. var __importStar = (this && this.__importStar) || function (mod) {
  3. if (mod && mod.__esModule) return mod;
  4. var result = {};
  5. if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
  6. result["default"] = mod;
  7. return result;
  8. }
  9. var __importDefault = (this && this.__importDefault) || function (mod) {
  10. return (mod && mod.__esModule) ? mod : { "default": mod };
  11. }
  12. exports.__esModule = true;
  13. var foo = __importStar(require("foo"));
  14. var bar_1 = __importDefault(require("bar"));

Numeric separators

TypeScript 2.7 brings support for ES Numeric Separators.Numeric literals can now be separated into segments using _.

Example
  1. const million = 1_000_000;
  2. const phone = 555_734_2231;
  3. const bytes = 0xFF_0C_00_FF;
  4. const word = 0b1100_0011_1101_0001;

Cleaner output in —watch mode

TypeScript’s —watch mode now clears the screen after a re-compilation is requested.

Prettier —pretty output

TypeScript’s —pretty flag can make error messages easier to read and manage.—pretty now uses colors for file names, diagnostic codes, and line numbers.File names and positions are now also formatted to allow navigation in common terminals (e.g. Visual Studio Code terminal).