Typescript: is it possible to 'spread' or merge interface into namespace?

217 views Asked by At

I'm working with a package that uses a namespace which holds all available variables and functions. Functions fall into two categories and I want consumer packages to have access to these categories separately. I want to suggest a change where both categories are separately exported, so they can be independently consumed.

The package looks like this:

declare namespace packagename {
  const myVar: string;

  // functions from category A
  function myFunctionA1(str: string): boolean;
  function myFunctionA2(nr: number): string;

  // functions from category B
  function myFunctionB1(str: string): boolean;
  function myFunctionB2(nr: number): string;
}

I want to suggest something like this, (both suggestions obviously don't work).

export interface CategoryAFunctions {
  myFunctionA1(str: string): boolean;
  myFunctionA2(nr: number): string;
}

export interface CategoryBFunctions {
  myFunctionB1(str: string): boolean;
  myFunctionB2(nr: number): string;
}

declare namespace packagename {
  const myVar: string;

  // spread functions from category A?
  ...CategoryAFunctions;

  // include functions from category B by key?
  [key in keyof CategoryBFunctions]: CategoryBFunctions[key];
}

..or some way that 'merges' both interfaces into the namespace, so that I can separately import CategoryAFunctions interface in my consumer package.

It's important that the resulting namespace doesn't change as other consumer packages may rely on the current one.

0

There are 0 answers