I am currently tasked with implementing a value transformation process that involves multiple steps. To ensure reusability of these steps, a proposed architecture allows for passing steps to the transformation function. For example, transforming a long string to uppercase and then truncating it:
Transformation.from("This is a long story").step(Uppercase).step(Truncate);
class Uppercase {
public run(str: string): string {
return str.toUpperCase();
}
}
class Truncate {
public run(str: string): string {
return str.substr(0, 10) + '...';
}
}
I am interested in finding a way (ideally at compile time) to ensure that the steps are compatible with each other. This means verifying that the output of one step is type-compatible with the input of the next step, preventing scenarios like the following where there is a mismatch in types:
class SquareRoot {
public run(num: number): number {
return Math.sqrt(num);
}
}
While this verification can be done at runtime by comparing input and output types, I am seeking a method to perform this check at compile time.