Skip to content

Instantly share code, notes, and snippets.

@championswimmer
Created May 3, 2018 08:31
Show Gist options
  • Save championswimmer/0e8eef7775097b7400b8c235c77a4221 to your computer and use it in GitHub Desktop.
Save championswimmer/0e8eef7775097b7400b8c235c77a4221 to your computer and use it in GitHub Desktop.
fetch_getters.ts
function Module<S> (module: Function & {state:any}) {
const state = new (module.prototype.constructor)({})
if (!module.state) {
module.state = <S>{}
}
const getters = Object.entries(Object.getOwnPropertyDescriptors(module.prototype))
.filter(([key, descriptor]) => typeof descriptor.get === 'function')
.forEach(([key,descriptor]) => console.log(descriptor.get.toString())
Object.keys(state).forEach((key: string) => {
console.log(state[key]);
if (state.hasOwnProperty(key)) {
if (typeof state[key] !== 'function') {
(module.state as any)[key] = state[key];
}
}
})
}
@Module
class Omg {
count = 0
get invCount() {
return this.count - 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment