Created
January 26, 2022 17:06
-
-
Save hasparus/b80c84a9702f1c6107d36ca7699cb2dd to your computer and use it in GitHub Desktop.
This file contains 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
const UNEXPECTED_ACCESS = Symbol() as never | |
export function throwsOnUnexpectedAccess<T extends object>(target: T): T { | |
return new Proxy(target, { | |
get: (target, key) => { | |
const value = target[key as keyof typeof target] | |
if (value === UNEXPECTED_ACCESS) { | |
throw new Error(`Unexpected access to "${String(key)}"`) | |
} | |
return value | |
}, | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment