Skip to content

Instantly share code, notes, and snippets.

@chris-kobrzak
Created November 11, 2022 12:01
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 chris-kobrzak/d5a00e12f38ca98fc7dd1bc4c792800c to your computer and use it in GitHub Desktop.
Save chris-kobrzak/d5a00e12f38ca98fc7dd1bc4c792800c to your computer and use it in GitHub Desktop.
JS decorators example
function readonly(target: any, name: string, descriptor: TypedPropertyDescriptor<() => void>) {
descriptor.writable = false
return descriptor
}
class Example {
a: number
@readonly
b() {}
constructor(a: number) {
this.a = a
}
}
const example = new Example(0)
example.a = 1
example.b = 2
// [ERR]: Attempted to assign to readonly property.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment