Ambient Classes and Functions Can Merge

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:

  1. export declare function Point2D(x: number, y: number): Point2D;
  2. export declare class Point2D {
  3. x: number;
  4. y: number;
  5. constructor(x: number, y: number);
  6. }

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

In TypeScript 3.7, the compiler will take advantage of this feature so that .d.ts files generated from .js files can appropriately capture both the callability and constructability of a class-like function.

For more details, see the original PR on GitHub.