Last active
August 13, 2020 08:44
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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