Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I reexport all imports from a module in typescript?

Tags:

typescript

I'm migrating a JS codebase to TS. In JS we have some index.js files which reexport all imports from a module:

export * as users from './users';

What's the equivalent TS for this?

like image 732
Patrick Finnigan Avatar asked Dec 24 '22 05:12

Patrick Finnigan


2 Answers

It is possible since TypeScript 3.8

Here's a summary of re-export *:

// Re-export *
export * as default from './example1' // as default-import
export * from './example2' // separately
export * as ex3 from './example3' // as a named-object

// usage:
// import ex1, { example2Const, ex3 } from './reexport'
// import * as all from './reexport'

And a reminder of re-export picked:

// Re-export chosen
export { a, b as newB, c as default } from "./example4"; // pick what to export

// usage:
// import c, { a, newB } from './reexport'
// import * as all from './reexport'

Docs:
· Re-export all
· Re-export as named object

like image 176
charles-allen Avatar answered Mar 16 '23 16:03

charles-allen


I haven't found a way to do this as a single line, but a coworker pointed out this approach can work:

import * as users from './users';

export {
  users,
};
like image 26
Patrick Finnigan Avatar answered Mar 16 '23 16:03

Patrick Finnigan