How to export a function as a class property from a module?
Even if modifiers such as public are added, when a class property points to a function within a module, it behaves as though it is private.
There are multiple ways to define a property (a, b, c in this example) that all achieve the same result, but none allow for defining the function in a separate module to prevent the class body from becoming too large.
// myModule.ts
/** This is a test */
export function func(msg) {
console.log(msg);
}
// myClass.ts
import * as myModule from './myModule';
export class MyClass {
a(msg) { return myModule.func(msg); }
b = (msg) => myModule.func(msg);
c = myModule.func;
}
// index.ts
import { MyClass } from './MyClass';
const classInstance = new MyClass();
classInstance.a('test'); // works, but cannot access typedefs from module this way
classInstance.b('test'); // achieves the same result as 'c', facing similar issue
classInstance.c('test'); // works, but TypeScript complains about property 'c' not existing on type 'MyClass'