I have a class that receive other classes of a know interface and a main method like this:
// BusinessLayer.ts
import getForm from "./getForm";
/*
* other import statements
*/
class BusinessLayer {
constructor(
protected cacheLayer: ICacheLayer,
protected databaseLayer: IRepositoryLayer,
protect serviceLayer: IServiceLayer,
) {}
async execute(arg1: number, arg2: string) {
const { dependencies, formTree } = await getForm();
...
...
...
return result;
}
}
To keep it clean I split this function into smaller one, each one in a file, for example the getForm()
// getForm.ts
import type BusinessLayer from "../BusinessLayer";
async function getForm(this: BusinessLayer) {
const formFromCache = await this.cacheLayer.getFormFromCache();
if (formFromCache) {
return formFromCache;
}
const formFromDB = await this.databaseLayer.getFormFromDatabase();
if (!formFromDB) {
throw new Error("Form Not Found");
}
const formValue = formFromDB.value;
return formValue;
}
export default getForm;
First I got a dependency cycle error, that was solved by the import type
on getForm.ts but now I'm getting
The 'this' context of type 'void' is not assignable to method's 'this' of type 'BusinessLayer'.ts(2684)
Aside from my comment. You need to call function with the context you want. Playground
const { dependencies, formTree } = await getForm.call(this);