I have the problem that I cannot access the attributes of my SteamSession instance in Composition. When I execute the main.ts, I get an undefined in the second console.log when I call getListings(), but I want the cookies attribute of SteamSession to be displayed there. That’s why I tried previously to define the class Listings as a child class of SteamSession, but this leads to a circular dependency in Typescript, combined with composition. (Both files are importing eachother in a infinum loop)
These are my two classes:
- SteamSession (Parent)
const Listing = require('./listing');
class steamSession {
public listingObj;
public cookies: string;
constructor() {
this.listingObj = new Listing();
}
setCookies(cookies: string) {
this.cookies = cookies;
}
}
module.exports = steamSession; // Export der Klasse
- Listing (Child)
class Listing {
getListings() {
console.log("Fetching Steam Session Listings");
console.log(this.cookies);
}
}
module.exports = Listing;
´
Main TS:
const SteamSession = require('./steamSession');
const session = new SteamSession();
session.setCookies("blablabla");
session.listingObj.getListings(); // Second Console.log prints out undefined
Usecase:
Since I am currently working on a Steam Bot that automates certain actions with Steam, I always need a SteamSession instance, without this no actions with the other classes such as Listing etc. can be performed. The class e.g. Listing needs the cookies from the SteamSession to work at all. All actions in the Listing class are therefore dependent on the SteamSession.
Therefore, when I create a SteamSession instance, I want to create the instance of the Listing class at the same time, because a Listing cannot exist without SteamSession.
In the end, steamsession is a single class that handles everything. The other child classes are only used for better structuring and implementation of the individual tasks, as each child class covers its own subject area.
2
Answers
Your use case calls for composition, not inheritance.
The latter shares structure of the parent class, but not the member values of a given parent instance.
But you want
Listing
to access the session (state) of aSteamSession
instance, so it needs to access a particular object.Your
undefined
error is because ofconsole.log(this.cookies);
.Your
Listing
class does not have acookies
property and is not a sub-class ofsteamSession
(which would be done usingextends
).It looks like you were having errors before using
extends
.If so, yes this would be correct via
circular dependency issues
.Note:
These issues are just ES6/JS classes related. Not necessarily TS specific, TS supports native JS
If your looking to just use basic inheritance for your
steamSession
class.You can:
class Listing extends steamSession {
public listingObj
from OR the justthis.listingObj = new Listing();
If you are looking for a solution more Typescript specific.
You probably want to use TS interfaces or Types to define your classes and using similar inheritance as the ES6 approach.
Hope this helps!