When working with Typescript, I encountered a situation where I had 2 functions containing the same function code. To streamline my code, I decided to extract this common function into its own file.
Currently, I have implemented it as shown below. However, I am contemplating whether I should instead create an abstract class that houses this function (as well as others that will be added soon). Alternatively, is it just as effective to leave it as it is currently?
File 1:
import { sharedFunction } from "../common/actions";
export async function onTrigger(context: Context, documents: Resource[] | null): Promise<void> {
sharedFunction(aParamFile1, bParamFile1)
}
File 2:
import { sharedFunction } from "../common/actions";
export async function onTrigger(context: Context, documents: Resource[] | null): Promise<void> {
sharedFunction(aParamFile2, bParamFile2)
}
Actions file containing the shared function:
export async function sharedFunction(input1: someType, input2: someType){
//Do something
}}
I am considering making it an abstract class because if I want to use several functions in File 1 or File 2, I would need to list them all in the import lines at the top. Is this considered untidy design or acceptable? Could it be avoided by using myAbstractClass.sharedFunction
instead?
However, I am unsure how the onTrigger
function can extend an abstract class since it is a function itself.
Any thoughts or advice on this matter would be greatly appreciated.
Thank you.