Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TS: Escaping reserved keyword and use as variable name

Tags:

typescript

Hope this isn't a duplicate, been searching around and not even really found an answer to whether this is possible or not, not to mention how, if that is the case that you can.

My problem is that I'm getting incoming json to my front with variable name "is-saved", how can I use it in my corresponding Typescript class, or can I? So I'm looking for a similar solution I'm doing in my backend (Java):

@JsonProperty("is-saved")
private Boolean isSaved;

And yes, I'm gonna get an answer from you that just change the name, but I can't, I don't have access to that actual code.

I've found some suggestions and I have tried the following, and do not work:

is-saved: Boolean //obviously!
"is-saved": Boolean
'is-saved': Boolean

Can anyone help?

like image 427
AT82 Avatar asked Sep 04 '16 17:09

AT82


1 Answers

Quoted string members are allowed in TypeScript. e.g. the following class works fine:

class Foo {
    'is-saved': boolean;
}

Can also be used in interfaces:

interface ServerData {
    'is-saved': boolean;
}
like image 159
basarat Avatar answered Nov 09 '22 22:11

basarat