Export everything but a few items in Typescript

Viewed 1383

I'd like to export everything from a module except for a few functions that I'd like to override. Something like this:

import * as Base from "base-module";
const extendMeOld = Base.extendMe;
delete Base.extendMe;

export * from Base;

export function extendMe() {
  extendMeOld();
  someOtherCode();
}

How can this be achieved with Typescript?

2 Answers

It's ugly, but this works:

import { extendMe as extendMeOld } from "base-module";

export function extendMe() {
  extendMeOld();
  someOtherCode();
}

// @ts-ignore
export * from Base;

I guess whatever is exported first takes precedence. I was surprised and expected it to be the opposite (export overwrite), but perhaps module resolution is a runtime thing?

Anyways, one overloads the other and tsc will get mad about it, but you can just make it ignore the problem.

In order to keep it tree shakable id suggest the following

export { doNotExtendMe, meNeigher } from "base-module";
import { extendMe as extendMeOld } from "base-module";

export function extendMe() {
  extendMeOld();
  someOtherCode();
}
Related