Issue
export class TimeSeriesDB {
private static inst: TimeSeriesDB;
public static db: InfluxDB;
public static connected: Promise<boolean>;
constructor() {
const url = Settings.get('INFLUX2_URL');
const token = Settings.get('INFLUX2_TOKEN');
TimeSeriesDB.db = new InfluxDB({
url,
token,
});
}
static async connect() {
return true;
}
static disconnect() {
// delete this.instance.db
}
static get instance(): TimeSeriesDB {
if (this.inst === undefined) {
this.inst = new TimeSeriesDB();
this.connected = this.connect();
}
return this.connected;
}
}
export default TimeSeriesDB.instance
The instance
getter function should return TimeSeriesDB
type but it returns Promise<boolean>
. This code compiles perfectly fine. Can someone tell me what is happening here? I expected the code above not to be compilable.
Solution
Type compatibility checking in typescript is based on structural equivalence. That means, for a type A
to be compatible to B
, on the instance (ie non-static) level there must not exist a required property or method in one of them, that does not exist in the other.
But your TimeSeriesDB
type does not have any required non-static properties or method. So when checking the structural equivalence between Promise<boolean>
and TimeSeriesDB
typescript is not able to find any property or method that is required in TimeSeriesDB
that does not exist in Promise<boolean>
. Thus, it considers the types as being structural equivalent and therefore it's allowed to return a Promise<boolean>
You will quickly find a compilation error if you do for instance the following
export class TimeSeriesDB {
private static inst: TimeSeriesDB;
public static db: InfluxDB;
public static connected: Promise<boolean>;
constructor() {
const url = Settings.get('INFLUX2_URL');
const token = Settings.get('INFLUX2_TOKEN');
TimeSeriesDB.db = new InfluxDB({
url,
token,
});
}
// Add this instance level method
public someMethod(): void {
}
static async connect() {
return true;
}
static disconnect() {
// delete this.instance.db
}
static get instance(): TimeSeriesDB {
if (this.inst === undefined) {
this.inst = new TimeSeriesDB();
this.connected = this.connect();
}
return this.connected;
}
}
export default TimeSeriesDB.instance
Then you will get an error telling you
Property 'someMethod' is missing in type 'Promise' but required in type 'TimeSeriesDB'.
Thus, your current problem will resolve itself (ie your compile will start to show errors) once you add some functionality (or at least data properties) to your currently completely non-functional, empty class.
Not sure, what your intentions with the current return this.connected
are (or if this is just a typo, and you are wondering why the compiler doesn't throw an error) so I can't really give an advice where to go from here. The logical fix would of course be return this.inst
instead of return this.connected
. A problem is of course still, that your connect
function is async
but your getter isn't. So if you do this.connect()
inside the getter, it won't await the result ...
Answered By - derpirscher
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.