Reputation: 2979
I have created this class:
export class ModelTransformer {
constructor(private readonly hostelTransformer: HostelTransformer) {}
static transform(hostel: Hostel): Hotel {
return hostelTransformer.transform(hostel);
}
}
But when I compile it I have this compilation error:
error TS2304: Cannot find name 'hostelTransformer'.
I also tried
static transform(hostel: Hostel): Hotel {
return this.hostelTransformer.transform(hostel);
}
but then I have the error:
error TS2339: Property 'hostelTransformer' does not exist on type 'typeof ModelTransformer'.
Upvotes: 0
Views: 54
Reputation: 35202
You can't access the instance properties inside static methods.
When you use private readonly hostelTransformer: HostelTransformer
in the constructor, it goes on the instance of the ModelTransformer
class. Like this
constructor(hostelTransformer) {
this.hostelTransformer = hostelTransformer;
}
You can't use return this.hostelTransformer.transform(hostel)
because transform
is a static method. When you call ModelTransformer.transform()
, this
will be ModelTransformer
and not the instance of the class.
If you want to use that property, you need to make the method non-static
Upvotes: 1