Skip to content

Instantly share code, notes, and snippets.

@thevtm
Created October 17, 2017 15:29
Show Gist options
  • Save thevtm/97a33963cf36c93865a36ee17e465ea4 to your computer and use it in GitHub Desktop.
Save thevtm/97a33963cf36c93865a36ee17e465ea4 to your computer and use it in GitHub Desktop.
Quick and dirty tricks for debugging Typescript
/*
* Orginial:
* Quick and dirty tricks for debugging Javascript by Chang Wang
* https://hackernoon.com/quick-and-dirty-tricks-for-debugging-javascript-d0e911c3afa
*
* Translated to Typescript by @TheVTM
*/
function print<T>(tag: string, param: T): T {
console.log(tag, param);
return param;
}
function traceFn<T extends Function>(fn: T, context?: any): T {
return <any>function() {
console.trace(`${fn.name} called with arguments: `, arguments);
return fn.apply(context || this, arguments);
}
}
function traceProperty(obj: object, prop: any): void {
let value = obj[prop];
Object.defineProperty(obj, prop, {
get () {
console.trace(`${prop} requested`);
return value;
},
set (newValue) {
console.trace(`setting ${prop} to `, newValue);
value = newValue;
},
})
};
print("print(): ", 212 + 123);
console.info = traceFn(console.info);
console.info("foo");
window._ = {};
traceProperty(window, '_');
window._ = 3;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment