Last active
May 22, 2018 13:19
-
-
Save Reshetnyak/e176d95331d739c06898c30151c6acf7 to your computer and use it in GitHub Desktop.
Object.keys behavior but with typings
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
// function getKeys<T>(obj: T): Array<keyof T> { | |
// let keys: Array<keyof T> = []; | |
// for (const key in obj) { | |
// keys.push(key); | |
// } | |
// return keys; | |
// } | |
function getKeys<T, K extends keyof T>(obj: T): Array<K> { | |
return <Array<K>>Object.keys(obj) | |
} | |
const k = getKeys({ a: 10, b: 20 }); | |
const o1 = { a: 10, b: 20 }; | |
const k1 = <Array<keyof typeof o1>>Object.keys(o1); | |
function getValues<T>(obj: T): Array<T[keyof T]> { | |
const values: Array<T[keyof T]> = []; | |
for (const key in obj) { | |
values.push(obj[key]); | |
} | |
return values; | |
} | |
const v = getValues({ a: 22, b: 'hello', c: [1, 2, 'he'] }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment