Skip to content

Instantly share code, notes, and snippets.

@AlexVallat
Last active October 18, 2018 08:41
Show Gist options
  • Save AlexVallat/5423ac89039f9c40420dda2e324c47d2 to your computer and use it in GitHub Desktop.
Save AlexVallat/5423ac89039f9c40420dda2e324c47d2 to your computer and use it in GitHub Desktop.
Type-safe object property value references
export interface IObjectValue<T> {
set(value: T): void;
get(): T;
}
export class ObjectValue<K extends keyof T, T> implements IObjectValue<T[K]> {
constructor(private readonly object: T, private readonly key: K) {}
set(value: T[K]) { this.object[this.key] = value; }
get() { return this.object[this.key]; }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment