As mentioned, using a barebone method call will have performance impact.
A better approach is to use a pipe, and you have best of both worlds. Just define a Cast pipe:
@Pipe({
name: 'cast',
pure: true
})
export class CastPipe implements PipeTransform {
transform(value: any, args?: any): Event {
return value;
}
}
and then in your template, use event | cast
when you need the cast.
That way, change detection stays efficient, and typing is safe (given the requested type change is sound of course).
Unfortunately, I don't see a way to have this generic because of the name
attribute, so you'd have to define a new pipe for each type.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…