Skip to content

Instantly share code, notes, and snippets.

@Reshetnyak
Created August 27, 2019 15:09
Show Gist options
  • Save Reshetnyak/a7f0f350ffce600ed564815bc4234682 to your computer and use it in GitHub Desktop.
Save Reshetnyak/a7f0f350ffce600ed564815bc4234682 to your computer and use it in GitHub Desktop.
TS helpers
enum Types {
Start = 'start',
End = 'end',
Hello = 'hello'
};
const freezed = Object.freeze({
Start: 'start' as const,
End: 'end' as const,
Hello = 'hello' as const
});
function ownKeys<
T extends object, //{[k: string]: string},
K extends keyof T
>(obj: T): K[] {
return Object.getOwnPropertyNames(obj) as any;
}
function ownValues<
T extends object, //{[k: string]: string},
K extends keyof T,
>(obj: T): T[K][] {
const keys = ownKeys(obj);
return keys.map(key => obj[key]) as any;
}
const own = Object.getOwnPropertyNames(freezed);
const ownP = ownKeys(freezed);
const ownV = ownValues(freezed);
const ownEnum = Object.getOwnPropertyNames(Types);
const ownEnumP = ownKeys(Types);
const ownEnumV = ownValues(Types);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment