UForm doesn't accept type import for schema

I have an exported const in a different file that has my zod schema. when I try to specify the schema in :schema in my UForm, I get this:
vue: Property formSchema does not exist on type
CreateComponentPublicInstanceWithMixins<ToResolvedProps<{}, {}>, { state: { title: string; description: string; objectives: string; }; onSubmit: (event: FormSubmitEvent<{ title: string; description: string; objectives: string; }>) => Promise<...>; }, ... 23 more ..., {}>
vue: Property formSchema does not exist on type
CreateComponentPublicInstanceWithMixins<ToResolvedProps<{}, {}>, { state: { title: string; description: string; objectives: string; }; onSubmit: (event: FormSubmitEvent<{ title: string; description: string; objectives: string; }>) => Promise<...>; }, ... 23 more ..., {}>
even though it works fine in other places
5 Replies
kapa.ai
kapa.ai2mo ago
Hi @Just Simeon. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave: ---------------------- This issue you're experiencing with UForm and type imports for schemas is related to a known problem in the Vue ecosystem. Let me explain the issue and provide a potential solution. The error message you're seeing suggests that the TypeScript compiler is not recognizing the formSchema property on the component instance. This is likely due to a change in how Vue types need to be augmented. To resolve this issue, you can try the following: 1. Make sure you're using the latest versions of your dependencies, especially Vue and related libraries. 2. If the issue persists, you may need to modify how you're declaring your types. Here's an approach that has worked for others:
declare global {
interface FormSchema {
// Your schema definition here
}
}

export {};
declare global {
interface FormSchema {
// Your schema definition here
}
}

export {};
This declares the type globally, which should make it available to the template. 3. Alternatively, you can try using the defineComponent function from Vue to define your component, which can sometimes help with type inference. If these solutions don't work, the problem might be related to how certain libraries are augmenting Vue types. As mentioned in this GitHub issue comment (https://github.com/nuxt/nuxt/issues/28707#issuecomment-2304235469), you may need to check if any of your dependencies are using the outdated declare module '@vue/runtime-core' syntax. To investigate this: 1. Search your node_modules folder for files containing declare module '@vue/runtime-core'. 2. If you find any libraries using this syntax, check if there are updated versions available that use the newer augmentation method. If you're still facing issues after trying these solutions, it might be helpful to create a minimal reproduction of the problem and share it with the Nuxt community for further assistance. Remember, while the UForm component is working correctly at runtime, this appears to be a TypeScript compilation issue related to type inference in the template. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@294593079812292609> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Just Simeon
Just SimeonOP2mo ago
@kapa.ai this is my schema: import {z} from "zod"; export const formSchema = z.object({ title: z.string(), description: z.string(), objectives: z.string() });
kapa.ai
kapa.ai2mo ago
Let me check the knowledge base for you again. One moment! :books: ---------------------- Based on the schema you've provided, I can help you with how to properly use it with the UForm component in Nuxt. The UForm component from the Nuxt UI library is designed to work with various schema validation libraries, including Zod. Here's how you can use your schema with UForm:
<script setup lang="ts">
import { formSchema } from './path-to-your-schema-file'
import type { FormSubmitEvent } from '#ui/types'

const state = reactive({
title: '',
description: '',
objectives: ''
})

async function onSubmit (event: FormSubmitEvent<typeof formSchema>) {
// Handle form submission
console.log(event.data)
}
</script>

<template>
<UForm :schema="formSchema" :state="state" @submit="onSubmit">
<UFormGroup label="Title" name="title">
<UInput v-model="state.title" />
</UFormGroup>
<UFormGroup label="Description" name="description">
<UInput v-model="state.description" />
</UFormGroup>
<UFormGroup label="Objectives" name="objectives">
<UInput v-model="state.objectives" />
</UFormGroup>
<UButton type="submit">Submit</UButton>
</UForm>
</template>
<script setup lang="ts">
import { formSchema } from './path-to-your-schema-file'
import type { FormSubmitEvent } from '#ui/types'

const state = reactive({
title: '',
description: '',
objectives: ''
})

async function onSubmit (event: FormSubmitEvent<typeof formSchema>) {
// Handle form submission
console.log(event.data)
}
</script>

<template>
<UForm :schema="formSchema" :state="state" @submit="onSubmit">
<UFormGroup label="Title" name="title">
<UInput v-model="state.title" />
</UFormGroup>
<UFormGroup label="Description" name="description">
<UInput v-model="state.description" />
</UFormGroup>
<UFormGroup label="Objectives" name="objectives">
<UInput v-model="state.objectives" />
</UFormGroup>
<UButton type="submit">Submit</UButton>
</UForm>
</template>
This setup should work correctly with your imported schema. The UForm component accepts the Zod schema directly in the :schema prop as shown in the Nuxt UI documentation (https://ui.nuxt.com/components/form#usage). If you're still encountering type errors, it might be related to how TypeScript is inferring types in your Vue template. In that case, you could try explicitly typing your component using defineComponent:
import { defineComponent } from 'vue'
import { formSchema } from './path-to-your-schema-file'

export default defineComponent({
setup() {
// Your setup logic here
return { formSchema }
}
})
import { defineComponent } from 'vue'
import { formSchema } from './path-to-your-schema-file'

export default defineComponent({
setup() {
// Your setup logic here
return { formSchema }
}
})
This approach can sometimes help TypeScript better understand the types being used in your template. Remember, while you might see TypeScript errors in your IDE, if the form is working correctly at runtime, it's likely just a type inference issue rather than a functional problem. ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ----------------------
kapa.ai
kapa.ai2mo ago
<@294593079812292609> Kapa.ai is still learning and improving, please let me know how I did by reacting below.
Want results from more Discord servers?
Add your server