I want to write the Arrow function in the array in typescript.

Asked 1 years ago, Updated 1 years ago, 371 views

How can I write the code below in typescript?

export default class Auth extensions Vue {
public rules: { username : Array < string | boolean > , password : Array < string > } = {
// I don't know how to write the type definition when I write the Arrow function in the array below.
// Currently, the type has not been declared, so there is an error.
        username: [
            v=>!!v||"User name is required",
            v=>(v&v.length>4)||"Username must be at least 5 characters long",
            v=>/^[a-z0-9_]+$/.test(v)|| "You have entered unauthorized characters"
        ]
        password: [
            v=>!!v||"Password Required",
            v=>(v&v.length>4)||"Username must be at least 5 characters long"
        ],
        }

}

typescript

2023-02-12 06:17

1 Answers

If you want to store function objects in an array, the second line is

public rules: {username:Array<Function>, password:Array<Function>} = {

Or

public rules: {username:Function[], password:Function[]} = {

You can write

If you want the arguments and return types of functions to be strict,

public rules: {username:(v:string)=>string|boolean)[], password:(v:string)=>string|boolean)[]}={

You can also write as shown in .
(You want the argument to be string and the return value to be string | boolean, right?)For example, if the argument is string | boolean, a type error occurs in the /^[a-z0-9_]+$/.test(v) part.)


2023-02-12 09:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.