Skip to content

Type error of v-for index #591

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

Closed
anbraten opened this issue Oct 14, 2021 · 6 comments
Closed

Type error of v-for index #591

anbraten opened this issue Oct 14, 2021 · 6 comments

Comments

@anbraten
Copy link

I get an strange error when running vue-tsc with my v-for type. Maybe this is related to #544

src/components/dashboard/ProjectsCard.vue:13:77 - error TS2322: Type 'number | keyof Project[]' is not assignable to type 'number'.
  Type 'string' is not assignable to type 'number'.

13       <ProjectsCardRow v-for="(project, index) in projects" :key="project._id" :index="index" :project="project" />
                                                                               ~~~~~
      <ProjectsCardRow v-for="(project, index) in projects" :key="project._id" :index="index" :project="project" />
<template>
  <router-link
    :to="{ name: 'project', params: { projectId: project._id } }"
  >
    <div>{{ index + 1 }}</div>
    <div class="font-bold">{{ project.name }}</div>
  </router-link>
</template>

<script lang="ts">
import { computed, defineComponent, PropType, toRef } from 'vue';
[...]

export default defineComponent({
  name: 'ProjectsCardRow',

  props: {
    index: {
      type: Number,
      required: true,
    },

    project: {
      type: Object as PropType<Model.Project>,
      required: true,
    },
  },

  setup(props) {
     [...]

    return {
      [...]
    };
  },
});
</script>
"vue-tsc": "0.28.2",
@johnsoncodehk
Copy link
Member

Please try config "lib": ["esnext", "dom"] to tsconfig, see https://github.com/vitejs/vite/blob/main/packages/create-vite/template-vue-ts/tsconfig.json

@johnsoncodehk johnsoncodehk added the question Further information is requested label Oct 14, 2021
@anbraten
Copy link
Author

Doesn't seem to be the case for us as we already had that:

{
  "extends": "../../tsconfig.json",

  "compilerOptions": {
    "lib": ["esnext", "dom"],
    "types": ["vite-svg-loader", "vite/client", "jest"],
    "typeRoots": ["src/@types", "node_modules/@types"],
    "composite": false,
    "outDir": "dist",
    "rootDir": ".",
    "baseUrl": "src",
    "paths": {
      "~/*": ["./*"],
      "$/*": ["../test/*"]
    }
  },

  "include": ["src", "test"],
  "references": [{ "path": "../core/tsconfig.build.json" }]
}

@johnsoncodehk
Copy link
Member

Your tsconfig working for me, please provide a minimal reproduce, thanks.

@mariusheine
Copy link

@johnsoncodehk The error still persists for me. I have a reproduction repo for vue-2 https://github.com/mariusheine/vue-tsc-type-error-v-for-index as well as for vue-3 https://github.com/mariusheine/vue-tsc-type-error-v-for-index/tree/vue-3

I used the tsconfig you linked above

@johnsoncodehk johnsoncodehk reopened this Apr 27, 2022
@johnsoncodehk johnsoncodehk removed the question Further information is requested label Apr 27, 2022
@johnsoncodehk
Copy link
Member

@mariusheine Thanks for the report. This is a bug of low TS version, please update your workspace TS version if you can, or use formatIndex(index as number) instead of.

@mariusheine
Copy link

@johnsoncodehk Sorry for the late reply. A ts version upgrade fixed it. Thx a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants