Check out the example you can copy and test in TypeScript Playground:
class GreetMessage {
message: {};
constructor(msg: string) {
let newTyping: { textMsg: string }; // reassign necessary
this.message = newTyping;
this.message.textMsg = msg; // typing issue
}
getMessage() {
return "Greetings, " + this.message.textMsg;
}
}
let greetMsg = new GreetMessage("universe");
let btn = document.createElement('button');
btn.textContent = "Show Greeting";
btn.onclick = function() {
alert(greetMsg.getMessage());
}
document.body.appendChild(btn);
In the code snippet above, there is an error with this.message.textMsg which requires reassigning {} to {textMsg: string} type.
Is it possible to achieve dynamic reassignment since I am uncertain about the type that will be used to populate the object during runtime (thus default typing) ?
Your assistance on this matter would be greatly appreciated. Thank you!