Skip to content

Instantly share code, notes, and snippets.

@hasparus
Created January 26, 2022 17:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hasparus/b80c84a9702f1c6107d36ca7699cb2dd to your computer and use it in GitHub Desktop.
Save hasparus/b80c84a9702f1c6107d36ca7699cb2dd to your computer and use it in GitHub Desktop.
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