I need to implement a getter and setter in my class. The setter should accept a querySelector
, while the getter is expected to return a new type called pageSections
.
The challenge I'm facing is that both the getter and setter must have the same argument/return value, but I want to include a type guard in the setter. The pageSections
type is well-defined in a separate type definition file.
// Example usage within the code ...
this.parent(this.closest('page-sections'))
// Class definition
PageSection {
private _parent: pageSections = undefined
/**
* @method setter parent
* @description Set the parent property
*/
set parent (parent: pageSections) {
if (this._parent === parent) return
if (typeof parent.current === undefined) return // This check ensures it is of type pageSections
this._parent = parent
}
/**
* @method getter parent
* @description Get the parent property
*/
get parent (): pageSections {
return this._parent
}
}
What could be missing here? How would you suggest approaching this issue?