I am facing an issue with initializing a decorated property "name" in a User class within the parent class constructor (Base) using Object.assign. The value ends up being "undefined" when the decorator is present, but it works correctly when the decorator is removed. This problem persists even though I am using TypeScript version 5.4.2.
Can someone provide assistance or guidance on how to resolve this issue and make it work as intended?
Thank you.
// test.ts
(Symbol as any).metadata ??= Symbol("Symbol.metadata") // Polyfill
function Value(value: string) {
return (_target: any, context: ClassFieldDecoratorContext) => {
context.metadata![context.name] = value
}
}
class Base {
constructor(data: any) {
Object.assign(this, data)
}
}
interface IUser {
id?: string
name?: string
}
class User extends Base implements IUser {
id?: string
constructor(data: IUser) {
super(data)
}
@Value("test")
name?: string
}
const data = {
id: "1",
name: "John Doe"
}
const user = new User(data)
// user.id is 1
// user.name is undefined