I am in the process of extracting an Angular pipe from an application to a library. The current pipe is tied to specific types, but I want to change it to use generic types while maintaining type safety.
Original 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 modify this code so that I don't have to rely on specific custom types when moving it to a library?
Revised Approach:
I have a requirement to keep the types out of the library. Would defining interfaces with specific/generic properties at the beginning of the file be considered bad practice - does this approach maintain 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 {
...