0

i have a function that talks to a server. And one of these getting parametes is called "userstate". This userstate is an object. I get from this object this:

userstate = {
    'username' = '',
    'display-name' = ''
};

How can I make a class of this? I have at the moment this:

extends class Userstate {
    username: string;
    displayName: string;

    constructior() {
        this.username = this['username'];
        this.displayName = this['display-name'];
    }
}

And in my function I do this:

let get_user_stuff = (userstate: Userstate) => {
    console.log(userstate.username);
    console.log(userstate.displayName);
}; 

And my console says this:

darky_chan
undefined

I hope you can help me :) Thanks in advance

  • not extends i wanted to write export –  Jun 01 '17 at 05:51
  • Possible duplicate of [How do I initialize a typescript object with a JSON object](https://stackoverflow.com/questions/22885995/how-do-i-initialize-a-typescript-object-with-a-json-object) – Amid Jun 01 '17 at 06:09
  • sry i didn't made a duplucate –  Jun 01 '17 at 06:21
  • Because i have a problem with that "-" in that object and i don't know how i make a object with that - to a class :/ –  Jun 01 '17 at 06:24
  • Ok - my bad - I see what your point is. – Amid Jun 01 '17 at 06:26

2 Answers2

0

Are you looking for something like this:

class Userstate {
    username: string;
    displayName: string;

    constructor(jsonObject: any) { // Notice the json object passed to constructor
        this.username = jsonObject.username;
        this.displayName = jsonObject['display-name'];
    }
}

let convertToInstance = (jsonObj: any) => (new Userstate(jsonObj));

let instance = convertToInstance({
    'username': 'name',
    'display-name': 'd-name'
});

console.log(instance);
console.log(instance.displayName);
Saravana
  • 37,852
  • 18
  • 100
  • 108
  • The problem is this: that i get a object with a -. I can't make of this class an object. I want to use this like, when I write it should show me what features it has. –  Jun 01 '17 at 06:41
  • For example: userstate. and now i get a list what it has. I don't want to make all the time this: userstate['display-name'], because i think this is looking bad –  Jun 01 '17 at 06:42
  • @Darky_Chan But you cannot access members with `-` without the `[member-name]` notation. So you cannot to `userstate.display-name` if that is what you want. I understand your JSON data has the hyphen but if you convert it to `Userstate` instance like I showed, you can access the instance using: `instance.displayName` – Saravana Jun 01 '17 at 06:50
  • mmh okay, but from where is this convertToInstance()? Sry maybe i just don't get it xD –  Jun 01 '17 at 07:00
  • Accidentally deleted the function when editing. It is added again. – Saravana Jun 01 '17 at 07:02
0

According to typescript specs you can have a property name containing invalid identifiers:

class Userstate
{
    username: string;
    "display-name": string;
}

The only problem is that you will have to access them using indexer:

const u = new Userstate();
u["display-name"] = 23;
Amid
  • 21,508
  • 5
  • 57
  • 54