Skip to content

Array get error type in v-for #1790

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
3 tasks done
yoyo930021 opened this issue Mar 17, 2020 · 6 comments · Fixed by #2248
Closed
3 tasks done

Array get error type in v-for #1790

yoyo930021 opened this issue Mar 17, 2020 · 6 comments · Fixed by #2248

Comments

@yoyo930021
Copy link
Member

yoyo930021 commented Mar 17, 2020

  • I have searched through existing issues
  • I have read through docs
  • I have read FAQ

Info

  • Platform: macOS
  • Vetur version: 0.24.0 (master)
  • VS Code version: 1.43.0

Problem

Wrong: readonly never[]
Correct: readonly { id: number; text: string; list: never[]; }[]

Screen Shot 2020-03-17 at 2 58 44 PM
Screen Shot 2020-03-17 at 2 58 56 PM

PS. show virtual file is correct.

Reproducible Case

https://github.com/yoyo930021/vetur-int-bug/blob/master/src/components/HelloWorld.vue

@ktsn
Copy link
Member

ktsn commented Mar 18, 2020

Hmm, that's weird. If I copy and paste the virtual code into the script block, it infers correct type.

@yoyo930021
Copy link
Member Author

Really weird.

I also check about virtual code in project.
https://github.com/yoyo930021/vetur-int-bug/blob/master/src/components/Test.vue

@ktsn
Copy link
Member

ktsn commented Mar 18, 2020

If it is with explicit interface annotation, the type will be correct:

import { defineComponent, computed } from '@vue/composition-api'

interface Group {
  id: number
  text: string
  list: string[]
}

export default defineComponent({
  setup() {
    const groups = computed<Group[]>(() => {
      const result = []
      result.push({
        id: 1,
        text: '',
        list: []
      })
      return result
    })
    return {
      groups
    }
  }
})

@yoyo930021
Copy link
Member Author

yoyo930021 commented Mar 18, 2020

This is also correct.

import { defineComponent, computed } from "@vue/composition-api";

export default defineComponent({
  setup() {
    const groups = computed(() => {
      const result: { id: number, text: string, list: string[]}[] = []
      result.push({
        id: 1,
        text: '',
        list: []
      })

      return result;
    });

    return {
      groups
    };
  }
});

I think problem when typescript automatically infer types.

@yoyo930021
Copy link
Member Author

yoyo930021 commented Mar 27, 2020

I found the problem.


When noImplicitAny: true or remove this line, array will get correct type.

I think we can remove this line for respect user settings.

@ktsn
Copy link
Member

ktsn commented Apr 12, 2020

Hmm, I see. I set noImplictAny for template service because user can write a function in template. e.g.:

<template>
  <button @click="(event) => onClick(event)">Click</button>
</template>

I guess we could just remove this option and filter implicit any errors out if it comes from template region.

yoyo930021 added a commit to yoyo930021/vuter that referenced this issue Sep 4, 2020
@yoyo930021 yoyo930021 mentioned this issue Sep 4, 2020
cereschen pushed a commit to cereschen/vetur that referenced this issue Sep 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants