0

I would like to add a getter and setter to my class. However the setter is supposed to receive a querySelector but the getter returns a new type pageSections.

My issue is that the getter and setter must have the same argument/return value, but I want to put the type guard in the setter. pageSections is defined in a type definition file and works fine.

// in the code …
this.parent(this.closest('page-sections'))

// in the class
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 validates it being a pageSections for now
    this._parent = parent
  }

  /**
   * @method getter parent
   * @description get the parent property
   */
  get parent (): pageSections {
    return this._parent
  }
}  

What am I missing? How should this be done?

Lukas Oppermann
  • 2,918
  • 6
  • 47
  • 62
  • `get/set` must return/accept the same type, this a restriction build into the compiler, no way around it. You can leave `get parent (): pageSections` and have a different property for set, for example `set parent_(parent: querySelector)`. A hack for sure but the only way to do it .. – Titian Cernicova-Dragomir Jun 27 '18 at 14:51
  • *"I would like to add a getter and setter to my class"* -- classes with public properties or getters **and** setters are procedural programming in disguise. Move the code that uses the getter and the setter into the class and you won't need getters and setters any more. – axiac Jun 27 '18 at 15:10

2 Answers2

1

You cannot do that and why should it be possible?

You can:

  • create another method (setParent(q:querySelector))
  • create a converter that elaborates the querySelector and returns a pageSections to set using "set parent"

You can find here an issue about it (still discussed from 2015).

Paolo Dell'Aguzzo
  • 1,431
  • 11
  • 14
  • Hmm, okay I am currently using the `setParent` method. It would be nicer to have it all in the setter, but if this is not possible, I guess this is the next best thing. – Lukas Oppermann Jun 27 '18 at 15:25
  • Yes @LukasOppermann. As you can read on GitHub is expensive for the TypeScript team and for someone is something that could create problems. – Paolo Dell'Aguzzo Jun 27 '18 at 15:30
0

You can try this package,

@p4ck493/ts-is

there is an example of how it can be used to check arguments,

@p4ck493/ts-is#-additional

but in your case you need to have a declared class on which the checks will be performed.

UDP:

In your example:

import {TypeGuard} from '@p4ck493/ts-type-guard';
import {is, RegisterInIs} from '@p4ck493/ts-is';

// in the code …
this.parent(this.closest('page-sections'))

// in the class
@RegisterInIs()
PageSection {
  private _parent: pageSections = undefined

  /**
   * @method setter parent
   * @description set the parent property
   */
   @TypeGuard([is.PageSection])
  set parent (parent: pageSections) {
    if (this._parent === parent) return
    // if (typeof parent.current === undefined) return // this validates it being a pageSections for now
    this._parent = parent
  }

  /**
   * @method getter parent
   * @description get the parent property
   */
   @TypeGuard({
    result: [is.PageSection]
  })
  get parent (): pageSections {
    return this._parent
  }
}

Or if you need it in the case of JavaScript, see the answer: https://stackoverflow.com/a/74675365/19306873

And yes, I am the author of the package, Harrison, thanks for your comment.

Karba
  • 1
  • 1
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 07 '22 at 08:52
  • If you are the owner of the linked package, you should state this in your answer. – Harrison Dec 08 '22 at 19:44