Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typings are not correct when using initialValues with undefined ref() #4139

Closed
2 of 5 tasks
sebastian-ludwig opened this issue Feb 15, 2023 · 4 comments · Fixed by #4064
Closed
2 of 5 tasks

Typings are not correct when using initialValues with undefined ref() #4139

sebastian-ludwig opened this issue Feb 15, 2023 · 4 comments · Fixed by #4064
Labels
🐛 bug Unintended behavior 👕 TypeScript TypeScript typings issue

Comments

@sebastian-ludwig
Copy link

sebastian-ludwig commented Feb 15, 2023

What happened?

I've defined a ref<MyInterface>() as initialValues that will be updated with loaded data in onMounted.
Unfortunately the form-values are not inferred correctly.
Please take a look here for demonstration:

interface Person {
  name: string;
  age: number;
}
const initialValues = ref<Person>();

const form = useForm({ initialValues });

onMounted(async () => {
  initialValues.value = { age: 45, name: 'Mike' }; // loaded via request
});

// form.values expected to be `{ name: string; age: number; } | undefined - but is `Record<string, any>`

Can form.values be inferred somehow by updating the typings?

Reproduction steps

  1. Use the Code-Sample above
  2. Inspect form.values in VS-Code

Version

Vue.js 3.x and vee-validate 4.x

What browsers are you seeing the problem on?

  • Firefox
  • Chrome
  • Safari
  • Microsoft Edge

Relevant log output

No response

Demo link

FYI: Unfortunately my Stackblitz-Demo (from below) is not working because of

  • Limited TypeScript Support to experience the issue
  • does not update form-values, which is really weird

https://stackblitz.com/edit/vee-validate-v4-starter-composition-example-zka5nq?file=src/App.vue

Code of Conduct

@logaretm
Copy link
Owner

Yep, this is a bug, I aim to solve this in #4064 as I discovered it there while working on the PR. I'm hard stuck with some things in Yup but should be tagged soon.

@sebastian-ludwig
Copy link
Author

Thanks @logaretm for those refactorings.
If I see it right, now I have to define a Partial-Ref for initialValues with an empty object, correct?
const initialValues = ref<Person>() --> ref<Partial<Person>>({});

But when I do it that way, the form-values are defined as required as you can see here, but age could be undefined:

Bildschirm­foto 2023-05-11 um 16 41 14

@logaretm
Copy link
Owner

logaretm commented May 11, 2023

Should NOT be required to be a partial ref, could be a partial of a non-ref. It is possible that value inference is not picking it up correctly since a lot of generics keep tossing the type around. will take a look over the weekend.

@logaretm logaretm reopened this May 11, 2023
@logaretm
Copy link
Owner

Sorry, it took longer than it should have. should be alright now with 4.9.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Unintended behavior 👕 TypeScript TypeScript typings issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants