vuejs / language-tools

⚡ High-performance Vue language tooling based-on Volar.js
https://marketplace.visualstudio.com/items?itemName=Vue.volar
MIT License
5.87k stars 404 forks source link

Unexpected type errors with vue-tsc 2.0.10 #4218

Closed kingychiu closed 3 months ago

kingychiu commented 7 months ago

Run

npm install
npx nuxi typecheck

in the following example:

https://stackblitz.com/edit/nuxt-starter-whhd9d?file=package.json

with vue-tsc 2.0.10:

Found 3 errors in 2 files.

Errors  Files
     2  app.vue:16
     1  node_modules/nuxt-time/dist/runtime/components/NuxtTime.vue:3

With vue-tsc 2.0.7: No errors

The first error is from this block of code:

<div v-if="myState?.arrayState" class="wrapper">
  <button @click="handle(myState.arrayState[0])">Add</button>
</div>

In vue-tsc 2.0.10, it raises an error about "myState.arrayState" is possibly undefined, but it is checked by the v-if already.

The second error comes from a node_module, which should be ignored.

Full error log from vue-tsc 2.0.10:

~/projects/nuxt-starter-whhd9d 35s
❯ npx nuxi typecheck
ℹ Compiled plugins/server.mjs in 3246.79ms                           01:31:04
ℹ Compiled types/plugins.d.ts in 3684.89ms                           01:31:04
ℹ Compiled plugins/client.mjs in 3808.14ms                           01:31:04
app.vue:16:30 - error TS18048: '__VLS_ctx.myState' is possibly 'undefined'.

16 

app.vue:16:30 - error TS18048: '__VLS_ctx.myState.arrayState' is possibly 'undefined'.

16 

node_modules/nuxt-time/dist/runtime/components/NuxtTime.vue:3:28 - error TS2307: Cannot find module '#build/nuxt-time-script.mjs' or its corresponding type declarations.

3 

Found 3 errors in 2 files.

Errors  Files
     2  app.vue:16
     1  node_modules/nuxt-time/dist/runtime/components/NuxtTime.vue:3
so1ve commented 7 months ago

Duplicate of https://github.com/vuejs/language-tools/issues/4209

kingychiu commented 7 months ago

@so1ve How about the issue within node_modules?

johnsoncodehk commented 7 months ago

@kingychiu Which file should #build/nuxt-time-script.mjs be resolved to? I can't find a nuxt-time-script.mjs file.

kingychiu commented 7 months ago

hi @johnsoncodehk, that mjs file is used by the nuxt-time package as shown in the reproduction. I think the issue here is more about we should not raise errors from a node module? because I have no control over it.

This error wont be raised in an older version of vue-tsc.

davidmatter commented 3 months ago

Hey there, as there have been many fixes in the last 2.x versions: Please let us know if you're still encountering this issue. Otherwise kindly close this one, thanks!

KermanX commented 3 months ago

The first reported error is expected. Expression in @click is wrapped in a closure, so that the type narrowing won't work, because myState.arrayState may be changed somewhere else.

The second reported error, I think, is the same as #4249. Let's track it there.