Need "nameof" alternative in TypeScript, trying to initialise a variable to the nameof it's own variable name

Viewed 264

Essentially I'm trying to find out how to create a readonly string that is equal to its own variable name in TypeScript. I can do this without issue in C# using the nameof function, but Typescript will not allow me to initialise a variable using itself.

In C# I have

public static readonly string Admin = nameof(Admin);

and that works fine, the value of Admin is "Admin".

I've tried the following in TypeScript

export class Resources {
    static nameof(variable: Object): string{
        const keys = Object.keys(variable);
        return keys[0];
    }

    static readonly Admin = Resources.nameof(Resources.Admin);
}

But that returns a "Property 'Admin' is used before its initialization." error.

Essentially I need variables that are equal to their variable names. I can't go into why I need them due to an NDA, but if anybody can give me some insights into a workaround, that would be wonderful.

0 Answers
Related