Encountering issues while passing variables in Vue 3 using TypeScript

While working on Nuxt 3, I encountered a bug with the following function:

My function,

const checkFileSize = (file: File): boolean => {
  if (!file) {
    return true;
  }

  return props.maxSize * 1024 * 1024 >= file.size;
};

This function is called when I upload a file.

However, when I console log the file mentioned above, it returns a Proxy object and consequently, I am unable to read file.size. The file.size property returns undefined.

I managed to resolve this by changing it to file.value.size, but there is a type error related to the function input (as interface File does not have any value key).

Do I need to change the type (in this case File => what?).
Or should I call my function somewhere else to get the exact file: File input?

I updated my code to use this method to call the handleChange function, in which I invoke checkFileSize:

const handleChange = (file: File) => {
     checkFileSize(file)
}

<v-file-input @change="handleChange" />

The issue of defining a type for the ref value still persists.

Answer №1

An issue occurred stating that 'ref' is being used as a type when it refers to a value.

To resolve the type error, use Ref:

const validateFileSize = (file: Ref<File>): boolean => {
  if (!file.value) {
    return true;
  }

  return props.maximumSize * 1024 * 1024 >= file.value.size;
};

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

I have a Visual Studio 2019 solution that consists of two projects - one is an Angular project and the other is written in TypeScript. I have successfully configured

We are currently utilizing Visual Studio 2019 (not the VS Code version) for our project. Within this solution, we have multiple projects included. One of these projects contains Angular code that we compile using the traditional 'ng build' comma ...

Produce configuration files on the fly for Angular Component Testing using @Component declarations

Looking to test an Angular 11 component: @Component({ selector: 'app-foo-page', template: ` <app-header mode='operational' cool='true'></app-header> Some content ` }) export class FooPageComponent { } ...

Methods for verifying an empty array element in TypeScript

How can I determine if an element in an array is empty? Currently, it returns false, but I need to know if the element is blank. The array element may contain spaces. Code let TestNumber= 'DATA- - -' let arrStr =this.TestNumber.split(/[-]/) ...

Exploring the attributes of optional features

Dealing with optional properties can be quite tedious. Consider the object test1 in TypeScript: interface Test { a?: { b?: { c?: { d?: string } } }; } const test1: Test = { a: { b: { c: { d: 'e' } } } }; Handling the absence of each proper ...

Suspend features for moving between pages in history

In the application, we have two main pages - Home.vue and Statistics.vue. The Home.vue page renders the TableFields.vue component. On the Home page, there are fields with numbers that have an initial value of "3" set upon page load. An interval running a c ...

Having trouble with Vue.js 2.5+ and Webpack 4+ not loading CSS files?

I am currently in the process of setting up a Vue project with webpack 4 enabled using the Microsoft SPA templates dotnet core as the base. Everything seems to be working fine, except for the fact that external CSS files are not loading into the project. I ...

Guide on transmitting data from a child component to a parent object in Vue.js?

When I attempt to utilize a child component with custom form inputs and emit those values to the parent component, I encounter an issue where one input value disappears when another input value is entered. Let me showcase some code: Child Component <tem ...

Creating an Introduction Slider in Ionic Vue Application: A Step-by-Step Guide

It's common to come across mobile applications that feature an introduction slider upon installation for the first time. I noticed that most of the information available online pertains to older versions of Ionic with Angular, which has left me feelin ...

ngx-bootstrap - Convenient time selection tool for forms

I'm currently working with the time-picker component provided by ngx-bootsrap and I am looking to integrate the time-picker into an input box in a manner similar to how it is done with the date-picker (which can be seen in date-picker) https://i.ssta ...

Ways to transfer information among Angular's services and components?

Exploring the Real-Time Binding of Data Between Services and Components. Consider the scenario where isAuthenticated is a public variable within an Authentication service affecting a component's view. How can one subscribe to the changes in the isAut ...

What is the process of generating an instance from a generic type in TypeScript?

Managing data in local storage and making remote API queries are two crucial aspects of my service. When defining a data model, I specify whether it should utilize local storage or the remote API. Currently, I am working on creating a proxy service that c ...

Keep only the selected properties in the object and eliminate all others

I am faced with a challenge where I need to eliminate array members that do not have a certain property. Take for example this scenario: const idToKeep = 1; const objList = [{ id: 1, name: 'aaa', }, { ...

Issue persists with Component functionality in IE 11 despite the implementation of Polyfill

Just starting to explore VueJS and attempting to make some code I discovered online work with Quill integrated into Vue. Everything runs smoothly on Chrome, but I'm struggling to get it to function properly in IE 11. I've experimented with polyf ...

`What specific type should be assigned to the custom styled input component in MUI?`

Hey team! Would you mind helping me out with this issue? The Mui documentation suggests setting a type for a Mui Styled component like this: const MyComponent = styled(MuiComponent)(({ theme }) => ({ // styling })) as typeof MuiComponent This method ...

Learn how to make a POST request using Fetch in Vuejs2

I am working on a form that includes a text field. When the button is clicked, I need to send a request with the value entered in the input field. Any suggestions on how to achieve this? <div class=" select__body" v-if="addedForm === 3 ...

Losing object instance in Angular 7 Typescript Service ErrorHandle

In an attempt to show an error message with [ngBootstrap Alert] upon receiving a 404 or 500 response from the Service API, I am encountering an issue. I intend to utilize the alertComponent for displaying errors and have employed the AlertService.ts for s ...

changing the function of a button dynamically in vuejs depending on the data present

I have a unique scenario in my vuejs sample where I am working on dynamically changing the button based on data from the vue model. For example, if the data returned indicates "video", I want to trigger an alert method. On the other hand, if it says someth ...

Dynamic Variable Typing in TypeScript: Adjusting variable types on the fly

I am working with 2 variables named locals and visitants. These variables can either be of type PlayerDto or TeamDto, which will be determined by a third variable called competitor_type. If competitor_type is player, then I need to assign a list of Players ...

What is the best way to dynamically display an array that is passed as a prop in vue.js?

Need help passing data to a child component in Vue. Currently able to access and render the array but wanting to render each object separately using a v-for loop. Parent Component: <template> <div> <h1>Welcome To Our Shop</h1 ...

RxJS equivalent of a 'finally' callback for Promises

In my component, I have a process that retrieves data from an API. To indicate whether the data is being loaded or not, I use a member variable called loading. Once the data retrieval process is complete, I set loading to false to hide the loading icon. Th ...