Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to escape and use reserved words in TypeScript class definitions?

Here's a simple example of what I'd like to do:

export class UserInfoPrivate {
    constructor(
        public repEmail: string,
        public repPhone: string,
        public agreedToTnC: boolean
    ) { }
}

export class UserInfo {
    constructor(
        public $uid: string,
        public orgApproved: false,
        public private: UserInfoPrivate, // <-- private is a reserved word
        public open: UserInfoOpen,
    ) { }
}

Since the datebase is a NoSQL (Firebase) and returns simple JSON structured objects, and I have a node under users called "private" I'd like to just map my class directly to it without extra parsing. Is there a way to use a reserved word such as "private" in a class definition or do I have to go change the node?

My research thus far seems to point to no but I have a hard time accepting that this just isn't possible.

like image 482
Methodician Avatar asked Dec 14 '25 15:12

Methodician


1 Answers

You can always put property names (in classes or object literals) in quotes - and for those that are not valid identifier names, you need to. You can access them using bracket notation then.

export class UserInfo {
    public "private": UserInfoPrivate
    constructor(
        public $uid: string,
        public orgApproved: false,
        privateParam: UserInfoPrivate,
        public open: UserInfoOpen,
    ) {
        this.private = privateParam;
    }
}

(I hope that looks like valid Typescript not only to my eye but also the compiler)

like image 58
Bergi Avatar answered Dec 16 '25 03:12

Bergi