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
Examples/documentation for using vuelidate with TypeScript #175
Comments
I personally never used it with typescript, and if You @jawnsy are able to figure that out, I'd be very happy to add that as official guide. Alongside type definitions contributed by @mrellipse. I'm personally using flowtype in my project and it's terrible enough in vue files that i'm mostly typing things outside of vue components. |
Hmm, I didn't actually try to use the definitions inside a single-file
component.
But based on what I know of the build chain, that error message simply
means the ts compiler hasn't included /src/types/index.d.ts
<https://github.com/monterail/vuelidate/blob/6086cf6937b635317a482ce3e8ee3c16a3890d01/src/types/index.d.ts>
file as a reference in compilation chain.
I would first check out if there is a 'verbose' option for whatever webpack
loader you are using to compile? That will give you clues about what paths
the compiler is probing in order to locate .d.ts and .ts source files.
Most ts loaders seem to load settings directly from tsconfig.json - so
hopefully it would just be a matter of playing around with that
include/exclude/files settings.
If you have the project posted online, just shoot me a link, and I am happy
to spend a bit of time trying to puzzle it out!
…On Fri, Aug 25, 2017 at 5:15 AM, Paweł Grabarz ***@***.***> wrote:
I personally never used it with typescript, and if You @jawnsy
<https://github.com/jawnsy> are able to figure that out, I'd be very
happy to add that as official guide. Alongside type definitions contributed
by @mrellipse <https://github.com/mrellipse>. I'm personally using
flowtype in my project and it's terrible enough in vue files that i'm
mostly typing things outside of vue components.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#175 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AF30XVgNSwqjkxm0qLYgqkoYF1fwjZx4ks5sbcvUgaJpZM4PBfoD>
.
|
Would be amazing to see a simple component typescript validation with vuelidate. Just a simple required or custom validaton ( let's say odd number ) added on a property in the component & not html. |
I'm finally playing with TypeScript with Vue on my own right now. With intention to eventually be able to develop those definitions and support them with confidence. Still, contributions highly appreciated 😉 |
@mrellipse Thank you for providing typescript definitions for vuelidate! If I may ask, how can I define custom validator with params? |
It's great to see vuelidate has got so much traction in the vue community! Just to clarify. The pull request I made to include typescript definitions never ended up being merged. And given my work and other ambitions at the moment, I couldn't commit to maintaining them. 😞 If someone wants to take it to use as an ongoing contribution, you have my 👍 @OvidiuBan, here is a screenshot. It shows suggestions on a component property based on vuelidate.d.ts |
Hi @mmajcenic if you have a look at the above screenshot, and then check out signup.ts if should put you on the right track to defining a custom validation. |
@mrellipse This looks amazing 😍 I wanted to reopen your PR but was unable to do so, as your repository got deleted. I'm definitely going to merge that though, especially now after playing a bit with typescript and understanding it a bit better. Ideally i'd like You to reopen it if possible. |
@mrellipse how did you do to have the suggestions? I put vualidate.d.ts in src/typings, but typescript and webpack don't seem to read it... |
@mrellipse BTW i'm on VScode with Typescript 2.5.3 and Webpack 3.6.0 |
What is the status of this? |
Well after looking at several potential validation packages, I decided this is what I wanted to go with before I realized there would be some challenges with Typescript and Vue Class Components. I'm not sure, but I'm guessing it might be at least a LITTLE smoother if you aren't using classes and just doing the newer After some fighting with it and looking at how This is pretty hacky and incomplete, but if others are really looking to use this with Typescript I'll try to find some time to finish out a more friendly version and open a PR. Oh and I've only attempted to deal with using this as a mixin, and hopefully obviously this is tested for all of a few minutes so far, but stepping away for a bit and thought I'd share first. Vue Class Component Usage:
Type Definitions: vuelidate.d.ts
|
@brianjorden Thanks for your work! I'm familiar with TypeScript, but I wouldn't call myself a wizard. However, looking over your defs, it should solve the errors I'm getting via tslint--but tslint obviously isn't recognizing the vuelidate.d.ts file I created at the root of my src tree. Basically, same issue as @victorgarciaesgi above (though I'm using PHPStorm and Webpack). Can you make a quick note where to store the vuelidate.d.ts file and how to config tslint to see it correctly? |
@bpongvh inclusion of the type file depends mostly on your tsconfig.json, mind sharing your current settings? Depending on your config, you may be able to throw it anywhere under your A consideration though, my current project setup is actually an extremely opinionated configuration to simplify a bunch of stuff for me, but isn't really meant for general usage. For example, I have several things globally declared, if I'm importing Vue into every file, do I really need to type that out every time? So I've tried to setup that example in a more general usage friendly manner. If your error is when referencing the Could you provide a simple example of your component definition, validations block, and how you are importing/including the mixin (globally or locally)? Also what version of Vue and Typescript? |
I'm using it with Typescript and I had to modify the typings a little bit! you can see it on one of my components but it works great!! (In my exemple I pass respective $v field to each input components to have independent errors logic) Basicaly declare it as a IValidator to have suggestions in Vscode @Prop({required: false}) $v: IValidator; |
@victorgarciaesgi completely off topic, but I was just glancing at your repo and think I see a bug. Your error messages have some references to a typo as the word |
@brianjorden Hey yeah thanks I forgot I had this error and I thought I fixed it! That's just the beginning of a student project so this is not the main focus :) |
@brianjorden Ok I had just mispelled "Length" with "Lenght", so frustrating aha |
@brianjorden @victorgarciaesgi Thanks for the notes! Got my setup to read the types now. I guess I had some other strange problem that was choking the ts compiler. Anyway, all works with whats defined in the types file. One note, however: I'm getting tslint complaints about $el not defined on Vue when I try to use Vuelidate as a global: "Property '$el' is missing in type 'VueConstructor.'" I studied the problem for a while, and I think it has to do with ...
My guess is it has to do with param types--maybe should be |
Hi guys, I had to give up using the abovementionned solution(s) after upgrading to TypeScript 2.6 and Vue 2.5+. |
@ozeebee It is inaccurate in a way that validator may just be any function that return boolean or promise. Nice that it's working for you guys and i'm going to soon roll out the typings into official package. |
@Frizi so how soon will you roll out the official typings? :) |
Looking forward to official typescript support! |
typings? |
Typings for the people ;) |
So when are we getting TS update? looking forward to it.... |
I'm also looking for forward to the TS update. |
PR on #287. Comments welcome. |
Is there any way to get around TypeScript compilation failure when calling
The only solution I found so far is to wrap if (this.$v) {
this.$v.$touch()
} PS |
That’s the only way as far I know to get around that ‘issue’ |
@types/vuelidate vuelidate#175
@wujekbogdan another solution, if you are sure |
I handled it with the example from the ticket creator. I moved my validations object out of the component class and added it into the @component decorator. After this, this.$v got defined. But it feels not correct. |
It should work fine after installing:
also you could try to delete node_modules dir and .lock file and ran npm install |
Hi, |
Be aware that the annotation is taken to "decorate" the validation() method into the component. To my knowledge, if you want to skip the annotation decorator thing, nothing is hindering you, though the typing won't be on your side. |
@janesser but when I move |
@jacek-jablonski In your main.ts import { Component } from 'vue-property-decorator'
Component.registerHooks(['validations']) Then on your components import { Component, Vue } from 'vue-property-decorator';
@Component
export default class Blank extends Vue {
validations() {
return {
...
}
}
}
} |
@victorgarciaesgi Thank you! |
@victorgarciaesgi thx, will do :) |
@victorgarciaesgi I still have trouble implementing it the way you showed. When looking through Vue.js devtools $v property is not present. But when I make some small change, that causes Vue to reload, it suddenly appears in computed properties. |
After updating dependencies in my package.json, I got the error: |
Before updating everything worked fine for me. Came back from vacation, should go to vacation again :D. |
Ok, it was a problem via npm install. After using yarn install on the vue cli, everything works fine. :) |
I am also having trouble getting Vuelidate to play nicely with Typescript -- I've followed multiple examples from various tickets or comments here including the nuxted based
My component looks like this currently.
|
@victorgarciaesgi Hi, EDIT: ok, I managed to solve my issue. It seems that it is important to register Component hooks in external file. After that, it just works. |
Hey @jacek-jablonski can you please share your environment please? If possible :) |
@Chris2011 of course, here it is: // registerComponentHooks.ts
import { Component } from "vue-property-decorator"
Component.registerHooks(["validations"]) // main.ts
import Vue from "vue"
import Vuelidate from "vuelidate"
import "./registerComponentHooks"
Vue.use(Vuelidate)
// ... and sample component: <template>
<b-form>
<b-form-group>
<label form="diplay-name">Diplay name</label>
<b-form-input id="display-name" v-model.trim="$v.modifiedAccount.displayName.$model" type="text"></b-form-input>
</b-form-group>
</b-form>
</template>
<script lang="ts">
import { Component, Prop, Vue } from "vue-property-decorator"
import { required } from "vuelidate/lib/validators"
import { Account, AccountModify } from "@/store/modules/accounts/models"
@Component
export default class AccountModifyForm extends Vue {
@Prop() readonly account: Account
private modifiedAccount: AccountModify = null
created() {
this.modifiedAccount = {
displayName: this.account.displayName,
email: this.account.email
}
}
validations() {
return {
modifiedAccount: {
displayName: { required }
}
}
}
}
</script> |
Just to add another example of using Single File Components, Typescript and Vuelidate togheter: <script lang='ts'>
import Vue from 'vue'
import { required, email, ValidationParams, ValidationRule } from 'vuelidate/lib/validators'
import Component from 'vue-class-component'
@Component({
name: 'ajuda',
validations: {
userEmail: {
required,
email
},
userMessage: {
required
}
}
})
export default class Ajuda extends Vue {
userEmail = ''
userMessage = ''
submitDataDummy () {
this.$v.$touch()
if (this.$v.$invalid) {
this.$swal({
titleText: 'Dados Incorretos',
text: 'Sua mensagem não foi enviada',
icon: 'error',
toast: true,
position: 'bottom',
timer: 5000,
timerProgressBar: true
})
} else {
this.$swal({
titleText: 'Obrigado',
text: 'Obrigado por entrar em contato conosco!',
icon: 'success'
})
}
}
} |
Weird change, haven't been able to nail it down yet: if I define it as a property on the object component, it works fine, if I use the |
Putting this:
In my SFCs fixed it, not really sure why I suddenly need to do this there too, but when I move to a full SPA this will probably go away. |
To add on the topic, I was experiencing the same Issue that @jacek-jablonski had, on page refresh $v wouldn't exist at all, but after any small code change a hot-reload would fire and $v object would appear.
|
I have the validation structure like this validations() {
people: {
required,
$each: {
name: minLength(3)
}
}
} my achievement is, I just want to get any auto completion in how do I do that? |
@irhamputra Can you provide the error you get and template part? |
Hi @FloryanFilip for replying. Actually I don't get any error in template, but in the script because Vuelidate instance from vuelidate module is not in the module and I just don't get the auto completion. What I want to achieve that I can get the reference type for |
I ran into a problem const validations = { Typescript swears at min property of object $param: "Property 'min' does not exist on type 'object'." Has anyone else experienced this and knows how to fix this issue? |
Hello!
First off, thank you to the Vuelidate creators for making this amazing little validation library. Second, thank you to @mrellipse for contributing type definitions in #47.
I have been trying to figure out how to use Vuelidate with TypeScript from the PR history, by perusing the PR code, and by just generally futzing around. I see that the changes should extend the Vue interface declaration with enough information to know that the
validations
field is an available option.However, I can't for the life of me figure out what I need to do to make this all play nicely. I tried using the
@Component
syntax from the tests (component.spec.ts) but get this error:corresponding to this code (mostly borrowed from the test file, and embedded in a
.vue
single file component):<script lang="ts">
</script>
Is there a guide for how to use Vuelidate with Vue Single File Components written in TypeScript? I've been trying to piece it together from the documentation embedded with the .ts code, but as I'm new to TypeScript and ES6, this has been a bit of a challenge for me.
The text was updated successfully, but these errors were encountered: