Home > Mobile >  Typescript creating an array of rules and getting
Typescript creating an array of rules and getting

Time:11-30

I am just starting out with Typescript in vue.js. I am trying to create an array of rules to validate an email.

in my data portion I have the following code

data(): {
    loading:  boolean,
    valid: boolean,
    emailRules: any[],
} {  
    return {
        loading: false,
        valid: true,
        emailRules: [
            v => !!v || "Email is required",
            v => /. @. \.. /.test(v) || "Email must be valid"
        ],
    }
},

I am getting a Parameter 'v' implicitly has an 'any' type error for the code above. I figured because it is a rule I could use any for my array type but I am wrong. So type should my array be so I don't have this error?

CodePudding user response:

You should type the v payload like :

        emailRules: [
            (v : string) => !!v || "Email is required",
            (v : string) => /. @. \.. /.test(v) || "Email must be valid"
        ],
  • Related