Created
August 27, 2019 15:09
-
-
Save Reshetnyak/a7f0f350ffce600ed564815bc4234682 to your computer and use it in GitHub Desktop.
TS helpers
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
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