Skip to content

Instantly share code, notes, and snippets.

@haxiomic
Created July 10, 2018 21:58
Show Gist options
  • Save haxiomic/a7e3f55d2729f40d860102106f637074 to your computer and use it in GitHub Desktop.
Save haxiomic/a7e3f55d2729f40d860102106f637074 to your computer and use it in GitHub Desktop.
Convert JS object to TS type
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