I'm relatively new to TypeScript and I've been trying to replicate a pattern I used in JavaScript where I would expose functions through a single object within a module (like "services"). Despite my efforts, I'm facing some issues when attempting to accomplish the same thing in TypeScript. Below is the simplified code snippet that's causing trouble:
class Tester {
public publicField = "public field";
private privateField = "private field";
public services() {
return {
service1: this.getThePrivateField
};
}
public getThePublicField() {
return this.publicField;
}
private getThePrivateField() {
console.log("reached getThePrivateField");
return this.privateField;
}
}
const t = new Tester();
console.log(t.services().service1()); // This returns undefined - but why?
Even though I can see that the code is reaching the getThePrivateField function because of the console.log statement inside it, the response being returned is undefined. Why is this happening?