Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Stage 3 Decorators not applying defineProperty #28712

Open
augustsaintfreytag opened this issue Apr 2, 2025 · 0 comments
Open

Stage 3 Decorators not applying defineProperty #28712

augustsaintfreytag opened this issue Apr 2, 2025 · 0 comments

Comments

@augustsaintfreytag
Copy link

augustsaintfreytag commented Apr 2, 2025

Deno 2.2.6
Node 23.6.1

Description

Defining a Stage 3 class property decorator that mutates its class property from data field to a getter/setter field seems to silently fail to mutate the type. The mutation the decorator does is effectively an Object.defineProperty(this, name, {…}) call. The get and set blocks are never called on the instance and inspecting the instance’s getOwnPropertyDescriptor also confirms that no get or set exists on the type.

When running the code with deno run file.ts, the decorator is effectively not applied at all. Compiling the exact same code with tsc and running it with node file.js works as expected.

Preview

export function InstanceProperty() {
	return function (_value: unknown, context: ClassFieldDecoratorContext) {
		const { name } = context
		const dataPropertyName = `__${String(name)}`

		return function (this: any, initialValue: unknown) {
			this[dataPropertyName] = initialValue

			Object.defineProperty(this, name, {
				get() {
					console.log(`Reading and returning value for property ${String(name)}, ${this[dataPropertyName]}`)
					return this[dataPropertyName]
				},
				set(newValue) {
					console.log(`Setting value for property ${String(name)} to ${newValue}`)
					this[dataPropertyName] = newValue
				},
				enumerable: true,
				configurable: true
			})

			return undefined
		}
	}
}

Reproduction

Here’s a reproduction: https://github.com/augustsaintfreytag/repro-deno-decorators

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant