Skip to content

Instantly share code, notes, and snippets.

@yannickglt
Created May 27, 2020 09:15
Show Gist options
  • Save yannickglt/c8062c2c0fb4ec72c9939d183a81b5b8 to your computer and use it in GitHub Desktop.
Save yannickglt/c8062c2c0fb4ec72c9939d183a81b5b8 to your computer and use it in GitHub Desktop.
Generic pure Angular pipe
// A safe way to call methods in templates
// Live demo @ https://stackblitz.com/edit/angular-generic-map-pipe
import { Pipe, PipeTransform } from "@angular/core";
// Methods passed to the map pipe cannot (and should not) use the `this` keyword from components. Otherwise, it would not be a pure pipe.
@Pipe({ name: "map" })
export class MapPipe implements PipeTransform {
public transform<T, R>(
thisArg: T,
project: (t: T, ...others: any[]) => R,
...args: any[]
): R {
return project(thisArg, ...args);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment