I'm trying to refactor out an Angular pipe from an application to a library. The pipe references specific types, but I'd like to convert this to use generic types while keeping its current type safety.
Pipe:
import { Pipe, PipeTransform } from ...
import { MyCustomTypeOne } from ...
import { MyCustomTypeTwo } from ...
import { MyCustomService } from ...
@Pipe({ name: 'customPipe' })
export class CustomPipe implements PipeTransform {
constructor(private myService: MyCustomService) {}
transform(value: MyCustomTypeOne, details: MyCustomTypeTwo): string {
return this.MyCustomService.getResult(
value.customPropertyOne,
value.customPropertyTwo,
details.customPropertyOne
);
}
}
How can I update this code in order to not have to reference the custom types when moving this to a library?
Update:
I have a business need to not move the types into the library. Would it be an anti-pattern to declare the types at the top of the file with specific/generic properties - does this allow me to keep the type safety?
Example:
...
interface MyCustomTypeOne {
customPropertyOne: any;
customPropertyTwo: any;
[key: string]: any;
}
interface MyCustomTypeTwo {
customPropertyOne: any;
[key: string]: any;
}
@Pipe({ name: 'customPipe' })
export class CustomPipe implements PipeTransform {
constructor(private myService: MyCustomService) {}
transform(value: MyCustomTypeOne, details: MyCustomTypeTwo): string {
...