Skip to content

Instantly share code, notes, and snippets.

@mcalavera81
Created September 23, 2020 17:02
Show Gist options
  • Save mcalavera81/7bda791bed6251af5c237a7ee3ef3b8f to your computer and use it in GitHub Desktop.
Save mcalavera81/7bda791bed6251af5c237a7ee3ef3b8f to your computer and use it in GitHub Desktop.
type ClassConstructor<T> = new (...args: any[]) => T;
function withEZDebug<C extends ClassConstructor<{ getDebugValue(): object }>>(
Class: C
) {
return class extends Class {
debug() {
let Name = Class.name;
let value = this.getDebugValue();
return Name + '(' + JSON.stringify(value) + ')';
}
};
}
class HardToDebugUser {
constructor(
private id: number,
private firstName: string,
private lastName: string
) {}
getDebugValue() {
return {
id: this.id,
name: this.firstName + ' ' + this.lastName,
};
}
}
let User = withEZDebug(HardToDebugUser);
let user = new User(3, 'Emma', 'Gluzman');
console.log(user.debug());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment