简体   繁体   中英

How to create an alias for an enum in TypeScript?

In my project I have a set of auto generated enum types, which I include from. Their names are long and I would like to created aliases for them. Let's say I have some enums with the following names:

MySuperLongEnumName1
MyEvenLongerSuperLongEnumName2

Now throughout the project these enums are used everywhere, but I'd like to replace them by easier names, without changing the auto generated file.

What I have now is the following code (unfortunately, it is not working):

import { MySuperLongEnumName1 as Enum1 } from 'auto-generated-file.ts';
import { MyEvenLongerSuperLongEnumName2 as Enum2 } from 'auto-generated-file.ts';

export {
    Enum1,
    Enum2,
};

So I would import them simply using:

import { Enum1 } from 'types.ts';

TypeScript seems to accept them and even adds a note when hovering over the type saying it is an alias . But the app crashes while starting, saying it loaded the auto-generated file twice (already declared). This error goes away when I removed the import / export for the alias.

This was actually working now the error in the other file has been resolved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM