首页 > 解决方案 > Re-exporting all members of a TypeScript namespace

问题描述

I'm trying to re-export everything in a namespace from a new module. I've tried the following, which works to a degree:

// index.ts
import * as foo from 'foo';

function myFunction() {}

// Re-export everything in 'foo' along with myFunction
const thing = {
  ...foo,
  myFunction,
}
export = thing;

This results in a type declaration as follows:

import * as foo from 'foo';
declare function myFunction();
declare const thing: {
    Foo: typeof foo.Foo;
    Bar: typeof foo.Bar;
    myFunction: typeof myFunction;
};
export = thing;

But when I try to consume the types from the generated d.ts file, I get an error:

'Foo' refers to a value, but used as a type

I suppose this is because Foo and Bar as declared as members of an object. Is there a way to achieve this re-export of existing types in a new module?

标签: typescript

解决方案


你试过这个吗?

export * from 'foo';
export function myFunction() {}

推荐阅读