Created
October 2, 2018 01:08
-
-
Save xdmorgan/61979bd7a3cc011c86f6a15bf0613efe to your computer and use it in GitHub Desktop.
Optional in nested Object
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 optional(obj: any, ...keys: string[]) { | |
let opt = obj; | |
while (keys.length && opt[keys[0]] !== undefined) { | |
const key = keys.shift(); | |
opt = opt[key]; | |
} | |
return keys.length ? false : opt; | |
} | |
const scrollHeight = optional(someDOMNode, "previousElementSibling", "scrollHeight"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment