exported enum/const is undefined within same file

494 Views Asked by At

Outdated original Question:

I have a big problem with typescript. I have a file (not class) with exported consts, enums, and functions.

Some functions are using the const and enums defined within the file it self. When debugging I found out that those are undefined which prevents the functions from working correctly.

When I access the enums and const from the "outside" they are working as supposed.

Here is one example:

export enum UltimateMeasures {
    ULTIMATE_CARE_1 = 1500,
    ULTIMATE_CARE_2,
}

export const UltimateMeasuresLabels: Map<UltimateMeasures, string> = new Map([
    [UltimateMeasures.ULTIMATE_CARE_1, "Ultima foo"],
    [UltimateMeasures.ULTIMATE_CARE_2, "Ultima bar"],
]);

export function ultimateMeasuresLabelProvider(key: any): string {
    if (Array.isArray(key)) {
        const labels = [];
        for (const k of key) {
            labels.push(UltimateMeasuresLabels.get(k));
        }
        return labels.join(", ");
    }
    return UltimateMeasuresLabels.get(key);
}

console.log(ultimateMeasuresLabelProvider(UltimateMeasures.ULTIMATE_CARE_1));

When ever I use UltimateMeasures from an different module, they are initialized, when I call ultimateMeasuresLabelProvider they are undefined within the function, so what can I do to make them defined to scopes without using classes and initializers?

fixed bug within call to get

UPDATE:

Sorry, but the bug was only presented by the debugger, which was not aware of the underlying implementation. Interesting to highlight, that if I would create an internal const awareOfUltimateMeasuresLabels and point it to the exported UltimateMeasuresLabels, also the debugger would work as supposed.

Thanks for the answers, I leave it here in case someone elses runs into the same situation to provide hope, that at the end, it works :)

export function ultimateMeasuresLabelProvider(key: any): string {
        const awareOfUltimateMeasuresLabels = UltimateMeasuresLabels;
        if (Array.isArray(key)) {
            const labels = [];
            for (const k of key) {
                labels.push(awareOfUltimateMeasuresLabels.get(k));
            }
            return labels.join(", ");
        }
        return awareOfUltimateMeasuresLabels.get(key);
    }

1

There are 1 best solutions below

0
paolostyle On

Your ultimateMeasuresLabelProvider function is not correct, in fact with strict mode enabled it wouldn't even compile. I'm not sure what exactly you're trying achieve but my best guess would be that you want to get one or multiple labels joined with comma set in the UltimateMeasuresLabels Map object by providing an UltimateMeasures value or an array of UltimateMeasures values. So I don't quite understand what's up with this: UltimateMeasuresLabels.get(UltimateMeasures[String(k)]) if you then call it like this: ultimateMeasuresLabelProvider(UltimateMeasures.ULTIMATE_CARE_1).

Either way this function most likely would fix your issues:

function ultimateMeasuresLabelProvider(key: UltimateMeasures | UltimateMeasures[]): string {
    if (Array.isArray(key)) {
        const labels: string[] = [];
        for (const k of key) {
            const label = UltimateMeasuresLabels.get(k);
            if (label) labels.push(label);
        }
        return labels.join(", ");
    }
    return UltimateMeasuresLabels.get(key) || '';
}

Note that you don't have to do any magic with converting key to String or anything like that.