I'm new to typescript and javascript, and I want to convert this code in JS
to its TS
version. When I try to access this
object in the typescript version, it says that 'this possibly be 'unknown'
or something like that.
Could you please help me to understand how you implement this code in TS
?
const singleton = {
instance: null, // socket.io instance
getInstance: (server) => {
if (!this.instance) {
this.instance = server; // takes 'Hello' as the value
}
return this.instance;
},
}
let a = singleton.getInstance('Hello');
let b = singleton.getInstance('World');
console.log(a === b); // true
console.log(a); // Hello
console.log(b); // Hello