Created
November 11, 2022 12:01
-
-
Save chris-kobrzak/d5a00e12f38ca98fc7dd1bc4c792800c to your computer and use it in GitHub Desktop.
JS decorators example
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
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