我有一個與服務器交談的功能。其中一個獲取參數被稱爲「userstate」。這個userstate是一個對象。我從這個對象得到這個:我該如何在打字稿中將一個無法識別的對象製作成一個類?
userstate = {
'username' = '',
'display-name' = ''
};
我該如何製作這個類?我此刻這樣的:
extends class Userstate {
username: string;
displayName: string;
constructior() {
this.username = this['username'];
this.displayName = this['display-name'];
}
}
而在我的功能我這樣做:
let get_user_stuff = (userstate: Userstate) => {
console.log(userstate.username);
console.log(userstate.displayName);
};
而且我的控制檯這樣說:
darky_chan
undefined
我希望你能幫助我:) 在此先感謝
不伸出我想寫出口 –
可能重複[如何用JSON對象初始化打字稿對象](https://stackoverflow.com/questions/22885995/how-do-i-initialize-a-typescript-object-with- A-JS on-object) – Amid
sry我沒有做過duplucate –