In short, I'm looking to pass a generic type into a factory's constructor and have the factory return instances of the generic type.
As a small bonus, the generic I'm passing in is a class extension itself. The goal is to have the AccountService.getOne() method return an instance of Account - which would then have access to it's particular methods.
The closest I've managed to get, is what you'll find below, where it's returning an instance of AccountService instead of Account
Please find an SSCCE below, I'm happy to answer any and all questions
Thanks in advance
PS: I have consulted a fair few resources on JSDoc so far, but the abstract examples aren't much help to me (yet)
https://github.com/google/closure-compiler/wiki/Generic-Types
https://medium.com/@antonkrinitsyn/jsdoc-generic-types-typescript-db213cf48640 (i know)
/**
* An abstract class representing a DB record
* @class
*/
class AbstractDataObject {
constructor() {}
save() {}
update() {}
delete() {}
}
/**
* An abstract service to retrieve DB records and return them as AbstractDataobjects
* @class
* @template T
*/
class AbstractDataService {
/**
* @param {T} classType The data class
*/
constructor (classType) {
this.classType = classType
}
/**
* @returns {T} Returns a new instance of the provided classType
*/
getOne () {
return new this.classType() // I assumed this would return it as an instance of the generic, alas
}
}
/**
* @class
* @extends AbstractDataObject<Account>
*/
class Account extends AbstractDataObject {
constructor () {
super ()
}
}
/**
* @class
* @extends AbstractDataService<AccountService>
*/
class AccountService extends AbstractDataService {
constructor () {
super (Account)
}
}
const accountService = new AccountService()
const account = accountService.getOne()
account. // Expect to see .save(), .update(), .delete() here, yet it is of type AccountService
from JSDoc return instance of generic type
No comments:
Post a Comment