import { Events } from "./events"; export class SettingsE { readonly name: string; readonly props: T; readonly events = new Events }, SettingsE>(); constructor(props: T, name: string = "settings") { this.props = props; this.name = name; } getProps(): T { return this.props; } setProps(props: Partial): this { Object.keys(props).forEach(p => ((this.props as any)[p] = props[p])); this.events.emit("", props as any); return this; } getProp(key: K): T[K] { return (this.props)[key]; } setProp(key: K, value: T[K]): this { (this.props)[key] = value; const props: Partial = {}; props[key] = value; this.events.emit(key as string, value); return this; } } // Test: npx ts-node src/settingse.ts // const x = { n: 2, s: "s" }; // const s = new Settings(x); // s.setProps({ n: 3 }); // console.log(typeof s.getProp("n")); // number // console.log(typeof s.getProp("s")); // string // s.setProps(JSON.parse(window.localStorage[s.name])); // s.events.any(data => (window.localStorage[s.name] = JSON.stringify(s.props)));