0

Stencil won't compile my components since the latest updates. The problem is the following:

@Element() el: HTMLSpxEditElement
@Listen('keydown', { target: this.el })
  onClickEnter (evt) {
    if (evt.keyCode === 13) {
      evt.preventDefault()
    }
}

Getting this error:

Object is possibly 'undefined'.

L49:      @Listen('keydown', { target: this.el })
L50:      onClickEnter (evt) {

I read through this thread here: How to suppress "error TS2533: Object is possibly 'null' or 'undefined'"? but changing "this.el" to "this!.el" is not helping either.

I don't want to change settings of the compiler.

Dennis
  • 318
  • 2
  • 11
  • If you know for sure `this.el` can never be null or undefined then just cast it to the appropriate type `{ target: (this.el as TypeIKnowItToBe) }`. Otherwise you'll have to check for null/undefined to satisfy the compiler. Read *all* of the answers on that question, there are plenty of ways to skin this particular cat. – Jared Smith Sep 29 '20 at 13:31
  • Does this answer your question? [How to suppress "error TS2533: Object is possibly 'null' or 'undefined'"?](https://stackoverflow.com/questions/40349987/how-to-suppress-error-ts2533-object-is-possibly-null-or-undefined) – Jared Smith Sep 29 '20 at 13:33

1 Answers1

0

I realised that the event listener didn't need to be specified, as it already applied to the host element.

@Listen('keydown')
  onClickEnter (evt) {
    if (evt.keyCode === 13) {
      evt.preventDefault()
    }
}

This fixed my issue.

Dennis
  • 318
  • 2
  • 11