Last active
October 18, 2018 08:41
-
-
Save AlexVallat/5423ac89039f9c40420dda2e324c47d2 to your computer and use it in GitHub Desktop.
Type-safe object property value references
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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