Reputation: 1274
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 {
...
Upvotes: 2
Views: 866
Reputation: 12960
The problem with this is that you are after certain properties of those types. So, for example, you have MyCustomTypeOne
and MyCustomTypeTwo
. Specifically, you are after .customPropertyOne
and .customPropertyTwo
.
This means that this pipe function can only work with types where those properties are there. With that being said, extracting these to be external to the library would just be a matter of migrating your models (MyCustomType*
) to be within your library opposed to outside of it.
This would mean that the only thing to update in your pipe function would be where you are importing the modules from.
The other thing here is that you mention using generics. I don't think you want to use generics here since that is more about the return type. This is a little subjective since there is a use case to use generics without actually having anything to do with the output. If you wanted to use generics, here is what that might look like:
transform<T, U>(value: T, details: U): string {
return 'something';
}
or
transform<T>(value: T, details: T): string {
return 'something';
}
But, remember when you do this, T
and U
are generic types. Someone could pass in a Date
object for example. This would break since the Date
object doesn't have a customPropertyOne
etc... The other thing to remember is that you never explicitly invoke the transform
function. Angular does. So, you'd never really get the opportunity to pass in your generic types.
I think you just want to move your models into your library opposed to making them "generic"
Upvotes: 1
Reputation: 177
types can be a part of the library, in this pipe scope.
I would suggest to create types inside the pipe, or by the pipe and move them together to the library.
also there is an option to use any instead of your specific type, which I wouldn't recommend
Upvotes: 1