Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Vue 2 template support #35

Closed
Shinigami92 opened this issue May 25, 2021 · 11 comments
Closed

Vue 2 template support #35

Shinigami92 opened this issue May 25, 2021 · 11 comments
Labels
question Further information is requested

Comments

@Shinigami92
Copy link

I want to use vue-tsc with currently a Vue 2 project due to I'm stuck on Vuetify

But sadly I get errors like <template v-for> key should be placed on the <template> tag. vue(32)

https://v3.vuejs.org/guide/migration/key-attribute.html#overview

This is correct for Vue 3, but not for Vue 2. Is it possible somehow to use a --vue=2 mode to prevent/suppress such errors?

De we need to report that to Volar instead of vue-tsc?

@Shinigami92
Copy link
Author

Shinigami92 commented May 25, 2021

Surprisingly I found a workaround 👀

I added following to my shims-vue.d.ts

declare module '@vue/runtime-dom' {
  type ReservedProps = {
    key?: string | number | symbol;
  };
}

Volar shows the error anyways

But that's okay, at least I can successfully build the project 🙂

@johnsoncodehk
Copy link
Owner

Thanks for the suggestion. But vue-tsc only report the TS problems, template problems report by vue compiler (vue-cli / vite) not by vue-tsc, so you can just ignore it.

Vue 2 template check may be supported by Plugin API.

@johnsoncodehk johnsoncodehk added the question Further information is requested label May 26, 2021
@Shinigami92
Copy link
Author

@johnsoncodehk I have now also some parts like this:

image

Is there an option to just say e.g. //- @ts-expect-error? Or just to disable ts-type for only template for now?

@johnsoncodehk
Copy link
Owner

You can use @ts-ignore in expression like this:

v-if="
  // @ts-ignore
  synonym.entity
"

And the problem is if you use v-if and v-for on single node, in vue 3 it's if > for, in vue 2 it's for > if.

You can define v-if on a single template node on vue 2 project to avoid this problem.

v-chip(v-for="synonym in item.synonyms", ...)
  template(v-if="synonym.entity")
    - ...

Note: vetur's eslint also report that don't use v-if and v-for on single node.

@Shinigami92
Copy link
Author

Shinigami92 commented May 27, 2021

I know the breaking change. But using v-chip(v-for) > template(v-if) was not an option here, due to I don't want empty chips rendered.
I needed a function to pre-filter the array/object.

@johnsoncodehk
Copy link
Owner

johnsoncodehk commented May 27, 2021

Is this better for you?

template(v-for="synonym in item.synonyms")
  v-chip(v-if="synonym.entity", ...)
    - ...

@Shinigami92
Copy link
Author

Sadly not, cause this would just move the error to the :key

template(v-for="synonym in item.synonyms")
  v-chip(v-if="synonym.entity", :key="synonym.id" ...)
                                ~~~~~~~~~~~~~~~~~ <template v-for> key should be placed on the <template> tag. vue(32)
    - ...

Letting me asked again to tell the compiler somehow to just ignore it, cause I know I'm using Vue 2 😅
And moving the :key up to template would result in rendering issues / runtime warnings in the browser console

@johnsoncodehk
Copy link
Owner

A good news is Vue 3.1 template compiler is available to Vue 2!

https://blog.ninja-squad.com/2021/06/07/what-is-new-vue-3.1/

@johnsoncodehk
Copy link
Owner

Please track vuejs/language-tools#346

@Shinigami92
Copy link
Author

Now that we have compat mode 2, could we add the symbol typedef automatically?

Currently I'm applying this patch via patch-package to suppress build errors.

diff --git a/node_modules/@vue/runtime-dom/dist/runtime-dom.d.ts b/node_modules/@vue/runtime-dom/dist/runtime-dom.d.ts
index f183ace..2d3b617 100644
--- a/node_modules/@vue/runtime-dom/dist/runtime-dom.d.ts
+++ b/node_modules/@vue/runtime-dom/dist/runtime-dom.d.ts
@@ -1398,7 +1398,8 @@ type EventHandlers<E> = {
 import * as RuntimeCore from '@vue/runtime-core'
 
 type ReservedProps = {
-  key?: string | number
+  // https://github.com/johnsoncodehk/vue-tsc/issues/35#issue-901304468
+  key?: string | number | symbol
   ref?:
     | string
     | RuntimeCore.Ref

@johnsoncodehk
Copy link
Owner

It seem 3.2.0-beta.8 already fixed it.

vuejs/core#4242

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants