简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM