Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Enum #30

Open
LarsLiden opened this issue Jul 6, 2017 · 6 comments
Open

Support for Enum #30

LarsLiden opened this issue Jul 6, 2017 · 6 comments

Comments

@LarsLiden
Copy link

Is there a way to support enum?

https://www.typescriptlang.org/docs/handbook/enums.html

@abobwhite
Copy link

abobwhite commented May 10, 2018

If your enum is a string enum, this will work out-of-the-box without any clazz. I just verified.

@evergreen-lee-campbell
Copy link

Using a string enum works for serializing, but not deserializing.

@cantide5ga
Copy link

Isn't this a limitation of Typescript string enums? An enum would come in as a string request but cannot be reverse mapped to its member.

@evergreen-lee-campbell
Copy link

If I specify that it should be mapped to my enum's clazz, I'd like to be able to reverse map to the member (as you might with, say, the EnumMember decoration in C#).

@abobwhite
Copy link

Maybe try https://github.com/typestack/class-transformer. I prefer that solution to this one anyway, however, I haven't yet tried enums with it

@cantide5ga
Copy link

cantide5ga commented Oct 25, 2018

I think it's important to repeat that Typescript itself cannot reverse map an enum based on a string.

One workaround my colleague @imperialyoyo implemented was through a converter:

export const getEnumConverter = (stringEnum: StringEnum, name: string): ICustomConverter => {
    return {
        fromJson(data) {
            if (isArrayOrArrayClass(data)) {
                return (data as string[])
                    .map((dataElement) => getEnumMatch(stringEnum, dataElement, name));
            } else {
                return getEnumMatch(stringEnum, data, name);
            }
        },
        toJson(data) {
            if (isArrayOrArrayClass(data)) {
                return (data as string[]).map((dataElement) => dataElement.toString());
            } else {
                return data ? data.toString() : null;
            }
        }
    };
};

const getEnumMatch = (stringEnum: StringEnum, data: string, name: string) => {
    if (data == null) {
        return null;
    }
    if (stringEnum[data]) {
        return stringEnum[data];
    }
    const enumValueMatch = getEnumValueMatch(stringEnum, data);
    if (enumValueMatch) {
        return enumValueMatch;
    }
    return data;
};

const getEnumValueMatch = (stringEnum: StringEnum, data: string): string => {
    const enumPropertyNames = Object.getOwnPropertyNames(stringEnum);
    const foundReverseSearch = enumPropertyNames.map((property) => stringEnum[property]).indexOf(data);
    if (foundReverseSearch > -1) {
        return stringEnum[enumPropertyNames[foundReverseSearch]];
    } else {
        return null;
    }
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants