Reputation: 141
I can't see (vs code/whisper) id
of my property when I am using mixins.
My code is:
class User {
// @ts-ignore
id: number;
}
function Parent<TBase>(Base: TBase) {
return class ParentChild {
_object: TBase;
constructor (o: TBase) {
this._object = o;
}
dump(): void {
console.log(this._object);
}
};
}
class Test extends Parent(User) {
}
const o = {id: 2} as any;
const i = new Test(o);
// problem
console.log(i._object.id);
Problem is on console.log(i._object.id);
line. I am getting an error: Property 'id' does not exist on type 'typeof User'
.
What is wrong and how can I fix it?
Upvotes: 3
Views: 339
Reputation: 1074266
By passing Base
as a parameter, you're using typeof User
(the constructor function type) as TBase
rather than User
(the type of User
instances). I think you just want User
, by specifying the generic argument:
class User {
// @ts-ignore
id: number;
}
function Parent<TBase>() {
// ^^^^^^^^^
return class ParentChild {
_object: TBase;
constructor (o: TBase) {
this._object = o;
}
dump(): void {
console.log(this._object);
}
};
}
class Test extends Parent<User>() {
// ^^^^^^^^^^^^^^
}
const o = {id: 2} as any;
const i = new Test(o);
// problem
console.log(i._object.id);
A couple of other notes:
as any
on o
.User
without implementation, use interface User { id: number; }
instead of class
. Then you won't need the @ts-ignore
. Playground link.Upvotes: 4