`Using <input type="range" v-model="state"> in Vue3/JSX is causing a data type mismatch as it writes a `string` value to my `number` model without

I am currently utilizing Vue 3 alongside TypeScript and JSX, not using Vue <template>s. When I incorporate an

<input type="range">
element to modify a model like this:

export default defineComponent({

  setup(props, { emit }) {
    const state = ref(123)

    return () => {
      <input type="range" v-model="state">
    }
  });
}

There is no type error shown during compilation. However, when the code is executed, changing the range input results in the transformation of state from holding a number to storing a string value. This violates the safety checks enforced by TypeScript and leads to bugs in other parts of the codebase.

  1. How can I prompt a type error at compile-time for this issue?

  2. What steps should I take to ensure only numbers are assigned to state?

I attempted using v-model.number, but it seems unavailable in JSX. Any suggestions?

My current workaround involves adding

onInput={() => { state = parseFloat(String(state)); }

for the conversion process. However, this method is prone to errors, and there may be concerns about watchers encountering the string version of the model before its conversion to a number.

Edit: Just to note, I'm utilizing the default Vite settings for this project's build configuration.

Answer №1

Utilizing the jsx plugin extension, you have the capability to execute a similar method :

<input v-model={[state, ['number']]} />

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Vue.js - Data is not displaying in console.log, but it is rendering correctly in the template

Within my Vue template, I am making asynchronous calls using async/await to fetch session data without needing to render it. Instead, this data is sent to another component as an array for producing graphs. To test this functionality, I added the array ses ...

Tips for anticipating a string that begins with a particular variable

One of the challenges I'm facing involves a simple function that creates a string from a complex object. To illustrate, consider the following implementation: public generateMessage(property: string): string { return `${property} more text.`; } ...

The value returned by the .then() function in Angular Promises

function performHTTPRequest(){ return new Promise((resolve, reject) => { this.http.get(this.url).subscribe(data => { resolve(data) }, error => { reject(error); }, ); }); } runAfte ...

Having trouble with deploying a Vuejs app from git on Jelastic

This is my first experience with Jelastic and I am attempting to deploy a Vue.js app from Git. After creating a Node.js environment and deploying my Vue.js app, I followed these steps: cd ROOT npm install npm run build I received a successful message th ...

What are some strategies for testing a dynamically loaded component in Vue?

Here is the code snippet of the component I am currently testing: <template> <component :is="content" /> </template> <script setup> import { defineAsyncComponent } from 'vue' import { useRoute } from 'vue ...

Encountering a problem when trying to assign a value to a file/image input field using Vue Formulate

Having trouble setting the initial value for the image input field? The documentation suggests providing an array of objects with URLs (link to docs). I've followed the same format for other fields like 'text' and 'email', which w ...

Steps to deactivating a styled button using React's styled-components:

I've created a very basic styled-components button as follows: import styled from 'styled-components'; const StyledButton = styled.button``; export const Button = () => { return <StyledButton>Default label</StyledButton> ...

The Vuebootstrap component b-alert is failing to display the dynamic message from the response variable returned by the axios

I have utilized Vue Bootstrap for styling, and I am in the process of creating a user registration form. Upon submission of the form by the user, Axios will make a POST request to the API. If the call is successful, the API will return a success response ...

Differences between using a getter in Vue.js/Vuex compared to directly accessing state values on the create lifecycle hook

As I utilize the created lifecycle hook within vue.js to fetch data from my store and pass it to a vue component's data, an interesting observation arises. When I employ this.selectedType = store.state.selectedType, the data is successfully retrieved ...

Webpack seems to struggle in locating imports that TypeScript seems to have no issue finding

Summary: I embarked on a project based on the well-functioning ASP.NET Core example using SignalR and webpack. My objective is to incorporate this TypeScript library for client-side interpolation in a server-driven multiplayer game. I suspect that the iss ...

Guide on utilizing the "window" attribute within Angular framework

Recently, I came across a neat "Scroll back to top" button that caught my eye: https://www.w3schools.com/howto/howto_js_scroll_to_top.asp Despite being new to Angular, I wanted to give it a try and implement this feature myself. However, every attempt I ...

Perfroming unit testing on base class using Jasmine and Angular framework

I have a common base class that I include in every grid component. Currently, I have the specifications for the grid component, but I want to create separate specifications for the base class in its own spec file. The goal is to eliminate redundant code ...

Guide to setting up an interface for returning events within a parameter

I am working with a component that has the following interface: interface IPreTicketListProps { onEditPreTicket: (preTicketId: string) => { preTicketId: string }; onCreateSuggestedOperation: (preTicketId: string) => { preTicketId: string }; } ...

Difficulty in binding large amounts of data with Angular causing Internet Explorer to become unresponsive

Whenever I try to bind the response of my service to a component variable, which contains more than 7000 records, my screen freezes and becomes unresponsive. This issue seems to occur only on Internet Explorer. Additionally, there are 3 dropdowns in the UI ...

The functionality of CSS transitions may be affected when dynamically adding a class

Creating a custom CSS for my main div: .main { height: 0%; position: absolute; width: 100%; z-index: 100; background: whitesmoke; bottom: 0; border-radius: 15px; padding: 20px; color: gray; left: 0; right: 0; transition: height 1s e ...

Best approach to inform pages about a variable update in Ionic

Imagine a scenario where we have a page called ListItemPage displaying a list of items: Within the ts file, there is a variable defined as items: any = []; In the html file, we find <ion-item *ngFor="let item of items"> Users can click on a (+ ...

Customizing event typings for OpenTok in Typescript

Currently, I am working on integrating chat functionality using the 'signal' events with the OpenTok API. Here is my event listener that successfully receives the signal: // Listen for signal CHAT_MESSAGE sess.on('signal:CHAT_MESSAGE', ...

How to refresh Google reCaptcha v3 in an Angular Firebase project

Recently, we encountered a problem with our application's signup process using OTP. We utilized Firebase phone authentication, which includes Google reCaptcha v3. While the OTP was sent successfully, we faced an error when attempting to change the pho ...

Creating an input field within a form in Angular 8 and associating it with a button - Troubleshooting

Hi there! I'm trying to add an input field along with a button to my form. Unfortunately, I'm encountering some errors in my code. Here's the link to my code on StackBlitz. The errors I'm facing include: Cannot find control with name: ...

Vue component utilizing named slots

Can this be done? Here is an example of what I have: <template> <my-layout> <template #header> Some header html goes here </template> Body html here </my-layout> </template> <scri ...