I having issue with getting properies of class when I'm passing that as parameter in another class method
My issue is so complicted in my code and I tried to simplified it to get the solution
imagine I have class as User like this:
class User {
name!:string
static setName(_name:string){
const user = new User()
user.name = _name
return user
}
}
and Company with almost same properties:
class Company {
name!:string
brand!:string = 'mtx'
static setName(_name:string){
const company = new Company()
company.name = _name
return company
}
}
and I have Registration Class that has register method:
class Registration {
static register<K >(name:string,Model: K & { setName:(n:string)=>K}): K{
return Model.setName(name)
}
}
and I register user and company in this way:
let user = Registration.register('mamad',User)
let company = Registration.register('mamad',Company)
so this is working for user but for company returns this error:
and here is playground
You can just remove the K &
so it will look like this:
class Registration {
static register<K >(name:string,Model: { setName:(n:string)=>K}): K{
return Model.setName(name)
}
}
When you are calling:
let company = Registration.register('mamad',Company)
You are not supplying K
argument, you are only basically supplying the setName
, because that is present as static property. If you would have the K &
part, then you would need to be supplying actual instance, new Company()
or such in order to be able to refer to instance fields.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.