javascript 参数"value“隐式具有”any“类型,typescript错误

fcwjkofz  于 2023-02-21  发布在  Java
关注(0)|答案(1)|浏览(431)

我想知道如何在数据中键入值变量。因为我得到了一个错误-参数“value”隐式具有“any”类型。

<template>
  <v-sheet width="300" class="mx-auto">
      <v-form fast-fail @submit.prevent>
        <v-text-field
          label="First name"
          :rules="firstNameRules"
        >{{ firstName }}</v-text-field>
        <v-btn type="submit" block class="mt-2">Submit</v-btn>
      </v-form>
    </v-sheet>
</template>

<script lang="ts">
export default {
  data: () => ({
      firstName: '',
      firstNameRules: [
        value  => {
          if (value?.length > 3) return true
          return 'First name must be at least 3 characters.'
        },
      ],
    }),
  }
</script>
7kqas0il

7kqas0il1#

就像在TypeScript中键入任何其他变量一样,使用variable: type语法:

export default {
  data: () => ({
    firstName: '',
    firstNameRules: [
      (value: string)  => {
        if (value?.length > 3) return true
        return 'First name must be at least 3 characters.'
      },
    ],
  }),
}

相关问题