Prevent "Object is possibly 'undefined'." using this.el in Stencil.js components

540 views Asked by At

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.

1

There are 1 answers

0
Dennis On BEST ANSWER

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.