Created
July 10, 2018 21:58
-
-
Save haxiomic/a7e3f55d2729f40d860102106f637074 to your computer and use it in GitHub Desktop.
Convert JS object to TS type
This file contains hidden or 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 getType(x) { | |
let type = typeof x; | |
if (type === 'object') { | |
if (Array.isArray(x)) { | |
return `Array<${getType(x[0])}>`; | |
} else { | |
let fields = []; | |
for (let key of Object.keys(x)) { | |
fields.push(`${key}: ${getType(x[key])}`); | |
} | |
return `{${fields.join(', ')}}`; | |
} | |
} else { | |
return type; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment