Skip to content

Instantly share code, notes, and snippets.

@Nillerr
Last active August 13, 2020 08:44
Show Gist options
  • Save Nillerr/87e24b7e5614bb0d1e796632cecad676 to your computer and use it in GitHub Desktop.
Save Nillerr/87e24b7e5614bb0d1e796632cecad676 to your computer and use it in GitHub Desktop.
Allows the use of keyof MyType type declarations in methods accepting the keys of the entries when transformed using this pipe in an *ngFor loop.
/**
* Transforms Object into an array of key value pairs.
*/
@Pipe({ name: 'entries' })
export class KeyValueEntriesPipe implements PipeTransform {
transform<T extends { [p in keyof T]: T[keyof T] }>(input: T): { key: keyof T, value: T[keyof T] }[] {
const entries: { key: keyof T, value: T[keyof T] }[] = [];
for (const key in input) {
if (input.hasOwnProperty(key)) {
entries.push({ key, value: input[key] });
}
}
return entries;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment