When using the Composition API in Vue 3, the "Exclude" TypeScript utility type may result in a prop validation error

Currently, I am utilizing Vue 3 alongside the Composition API and TypeScript, all updated to their latest stable versions.

If we take a look at the types below:

export interface Person {
    name: string;
}

export type Status = Person | 'UNLOADED' | null;

My goal now is to utilize Status as a prop in a component, but excluding the possibility of null since validation has already been performed in the parent component, making redundant to validate null again.

In order to achieve this, we can make use of the Exclude utility type:

<script setup lang="ts">
const props = defineProps<{
    status: Exclude<Status, null>;
}>();
</script>

Upon implementing this, all validations within the component function correctly.

However, upon running the application and passing 'UNLOADED' as the prop value, a warning is displayed:

[Vue warn]: Invalid prop: type check failed for prop "status".
Expected Object, received String with value "UNLOADED".

This led me to try translating it to the Options API. Surprisingly, the following declaration worked flawlessly:

<script lang="ts">
import {defineComponent, PropType} from 'vue';

export default defineComponent({
    props: {
        status: {
            type: [Object, String] as PropType<Exclude<Status, null>>,
            required: true,
        },
    },
});
</script>

Hence, it appears that in the Composition API, Vue always considers that Exclude returns an object, resulting in complaints about a nonexistent prop validation error when a string is passed (which is not treated as an object).

Could this be a bug?

How can I address this issue using the Composition API?

Answer №1

Starting from Vue 3.3, this syntax is now considered valid.

export interface Person {
    name: string
}
export type Status = Person | "UNLOADED" | null
const props = defineProps<{
    status: Exclude<Status, null>
}>()

< Before Vue 3.3

If you are using the composition API, your prop declarations should look like this :

<script setup lang="ts">
import { PropType} from 'vue';

const props = defineProps({
    status: {
            type: [Object, String] as PropType<Exclude<Status, null>>,
            required: true,
        }
});
</script>

The usage of defineProps<SomeType>() only allows for:

  • A type literal
  • A reference to an interface or a type literal in the same file

At present, complex types and type imports from external files are not supported. However, there may be support for type imports in future updates.

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'm having trouble linking MikroORM migration to Postgresql - the npx command keeps failing. Can anyone offer some guidance on what

I am encountering a situation similar to the one described in this post. I'm following Ben Awad's YouTube tutorial: you can see where I am in the tutorial here. Objective: My goal is to execute npx mikro-orm migration:create in order to generate ...

Enhance user experience by enabling clickable links in Angular comment sections

Currently, I'm developing an application that allows users to add comments to specific fields. These comments may contain links that need to be clickable. Instead of manually copying and pasting the links into a new tab, I want the ability to simply c ...

Discovering the key to selecting a row by double-clicking in Vuetify's v-data-table

I'm having trouble retrieving the row by event in my v-data-table. It only gives me the event and remains undefeated. How can I catch items in the v-data-table? <v-data-table :headers="showHeaders" :page="page&quo ...

Incorrectly selecting an overload in a Typescript partial interface can lead to errors

My attempt to define an overload for my function using a Partial interface overloading is causing typescript to select the incorrect overload interface Args { _id: string; name: string; } interface Result { _id: string; name: string; } function my ...

Nuxt.js encountered an unexpected keyword 'export' causing a parsing error

Within my index.js file in the 'store' section, I am encountering the following code: import Vuex from 'vuex' // import axios from 'axios' const createStore = () => { return new Vuex.Store({ state: { loadedPost ...

After initializing a Vue instance, the textContent of various HTML elements is obtained

Before and after creating a Vue instance over an element, different values of textContent are retrieved. Click here to see the issue in action To observe the varying outputs, check the console. The white space characters are being stripped off by the Vu ...

When working with Vue API composition, is it recommended to utilize `ref` within the `

When using reactive in Vue API composition, should I opt for ref or not? Are they equivalent? Does reactive perform the same function as ref? const state = reactive({ loading: ref(true) }); or const state = reactive({ loading: true }); ...

Extremely sluggish change identification in combination Angular application

We are encountering consistent issues with slow change detection in our hybrid AngularJS / Angular 8 app, especially when dealing with components from different versions of the framework. The problem seems to arise when using older AngularJS components wit ...

Running ngAfterViewInit() code in Angular should be done only after Inputs() have been initialized

Within a particular component, I have implemented some code in ngAfterViewInit: @Input public stringArray: string[]; public newArray: string[]; ngAfterViewInit() { this.newArray = this.stringArray.filter(x => x.includes('a')); } I placed ...

Adding a total property at the row level in JavaScript

Here is a JavaScript array that I need help with: [{ Year:2000, Jan:1, Feb: }, {Year:2001, Jan:-1, Feb:0.34 }] I want to calculate the total of Jan and Feb for each entry in the existing array and add it as a new property. For example: [{ Year:2000, Ja ...

How can I display only a single quote instead of all the API data in a Vue 3 script setup?

Working on a simple quote generator using the composition API with script setup in JavaScript. The issue I'm facing is that all quotes are displayed at once when the button is pressed. How can I modify it so only one random quote is shown each time? & ...

What is the best way to retrieve an object when a value is not found? Consider implementing a looping mechanism with a specific condition in React and TypeScript to successfully

Greetings, I am faced with an array of objects structured as follows: const arr_obj = [ { id: '1', jobs: [ { completed: false, id: '11', run: { ...

The validationSchema function argument value in vee-validate@next is consistently observed to be undefined

I recently created a simple login form in Vue 3 and I'm currently attempting to validate it using the vee-validate@next composition API method. You can view the code on stackblitz: https://stackblitz.com/edit/vue-zauhqb However, I've encountered ...

Discovering the category for ethereum, provider, and contract

My current interface looks like this: interface IWeb3 { ethereum?: MetaMaskInpageProvider; provider?: any; contract?: any; }; I was able to locate the type for ethereum using import { MetaMaskInpageProvider } from "@metamask/providers", ...

"Exploring the world of mocking module functions in Jest

I have been working on making assertions with jest mocked functions, and here is the code I am using: const mockSaveProduct = jest.fn((product) => { //some logic return }); jest.mock('./db', () => ({ saveProduct: mockSaveProduct })); ...

"Vue.js: The Ultimate Guide to Event Management and Data Handling

I recently started learning Vue.js and I'm having some difficulty with my coding exercises: The task is to have a menu button that opens a dropdown box when clicked, and when any selection is made, it should go back to the menu button. index.js cons ...

What are the best methods for implementing runtime type checking in JavaScript?

Utilizing either TypeScript or Facebook's Flow (type), I am empowered to statically assign types to variables like this: function add (x: integer, y: integer) { ... } Both TypeScript and Flow are able to identify and prevent incorrect invocations su ...

TypeScript primitive type is a fundamental data type within the

Does TypeScript have a predefined "primitive" type or similar concept? Like type primitive = 'number' | 'boolean' | 'string';. I'm aware I could define it manually, but having it built-in would be neat. ...

What is the most reliable way to create an array ensuring that all potential values come from a specific dictionary?

I am seeking a method to define the testArray so that only keys from the example dictionary can be inserted into the array. enum example { key1 = 'A', key2 = 2, key3 = '3', }; const testArray: ?? = [example.key1, example.ke ...

Using TypeScript to set an HTMLElement in a web page

Currently in the process of transitioning my old JavaScript code to TypeScript. One of the components I have is a Table Of Contents JSX component that helps me navigate easily to specific headings. I had a function that calculated the total offset needed ...