By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – How to type the event from on:input (from HTML Input element in Svelte) with JSDoc?
JavaScript

Javascript – How to type the event from on:input (from HTML Input element in Svelte) with JSDoc?

Admin
Last updated: 2024/01/13 at 12:37 PM
Admin
Share
3 Min Read
How to type the event from on:input (from HTML Input element in Svelte) with JSDoc?

Problem:

I’m trying to type an event that I receive from a Svelte-managed input element, but TypeScript tells me that my JSDoc type is incompatible with what I receive from the on:input. In my page code I have an input field with the following handler:

Contents
Problem:Solution:
<input id="description" on:input={handleInput}

And in JavaScript I have:

/** @param {InputEvent & { target: HTMLInputElement}} event */
const handleInput = (event) => {
  const fullInput = event.target.value;
  const singleChar = event.data;
  const cursorPosition = event.target.selectionStart;
  // ...

In handleInput() the types are recognised correctly. But TypeScript shows this error on on:input in the HTML:

Type '(event: InputEvent & {    target: HTMLInputElement;}) => void' is not assignable to type 'FormEventHandler<HTMLInputElement>'.
  Types of parameters 'event' and 'event' are incompatible.
    Type 'Event & { currentTarget: EventTarget & HTMLInputElement; }' is not assignable to type 'InputEvent & { target: HTMLInputElement; }'.
      Type 'Event & { currentTarget: EventTarget & HTMLInputElement; }' is missing the following properties from type 'InputEvent': data, dataTransfer, inputType, isComposing, and 5 more.js(2322)

Why are these types incompatible and what can I do about this?

Solution:

This is the JSDoc @param definition to use:

/** @param {Event & {currentTarget: EventTarget & HTMLInputElement} & {target: HTMLInputElement} & InputEvent} event */

Explanation:
Just to see the type change on:input to on:input={(e)=> e } and hover over e. You will get the exact type that is expected (copy/paste-ready for JSDoc @param).

In this case it was:
{Event & {currentTarget: EventTarget & HTMLInputElement}

This solves the TS error on on:input, but it creates TS errors on:

const fullInput = event.target.value;
const singleChar = event.data;
const cursorPosition = event.target.selectionStart;

Because .data and .target is not a property on Event, but on InputEvent. See explanation why Svelte is not using InputEvent here. And .target should be typed as HTMLInputElement (like .currentTarget) By adding & {target: HTMLInputElement} & InputEvent you can add these types and TS doesn’t complain any more. Not sure if this is good practice though.

Hope this helps others. Thank you Mike ‘Pomax’ Kamermans and jonrsharpe and the Svelte Discord channel.

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How To Extract content from pfd file using javascript Javascript – How To Extract content from pfd file using javascript
Next Article Javascript Regex for allowing alphanumeric Characters,comma, dots, spaces Javascript – Javascript Regex for allowing alphanumeric Characters,comma, dots, spaces
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?