17

I have an enum declared somewhere in a sub module, and I'd like to re-export it in my actual project.

module :

export enum name {
  TOTO = "toto",
  TITI = "titi",
}

export :

import { name } from "module"
export type name2 = name

index.ts:

switch (var) {
  case name2.toto: // 'name2' only refers to a type, but is being used as a value here.
}

How can I not lose information that name2 is initially an enum ?

1
  • const name2 = name Commented Aug 28, 2018 at 15:00

2 Answers 2

28

Your re-export should be:

export { name as name2 };

This can be used with any kind of declared name: variable, enum, class, etc.

Sign up to request clarification or add additional context in comments.

Comments

9

The suggested method didn't work for me, as I import names like this:

import * as API from './__generated__/api/data-contracts.ts' 

In such circumstances, one doesn't simply export it as:

export { API.name as name2 };

because it's an error.

Luckily, I've finally learned a way to do it, thanks to [email protected]#javascript:

export type name2 = API.name;
export const name2 = API.name;

This way, we get the best of both worlds - types and values. And TS will figure out which one it wants.

1 Comment

This is great! It works also if you like to reexport a member of a namespace. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.