vue-codemod icon indicating copy to clipboard operation
vue-codemod copied to clipboard

Vue class based to vue2 with TS support

Open matrunchyk opened this issue 3 years ago • 10 comments

matrunchyk avatar Nov 24 '21 12:11 matrunchyk

Fixes: https://github.com/vuejs/vue-codemod/issues/33

matrunchyk avatar Dec 27 '21 13:12 matrunchyk

Nice! We've to migrate a bunch of components from class to composition functions on our project. I'll start from this, it helps a lot. Thanks ;)

rndmerle avatar Feb 28 '22 11:02 rndmerle

@matrunchyk I've a bunch of TS errors when trying to build, like Property 'init' does not exist on type 'Identifier' any idea? That's ast-types typings. I tried with npm and yarn, and with node@12 also. Since @types/jscodeshift is still 0.7.1 I don't get what's the issue. I tried to upgrade @types/jscodeshift to 0.11.0, or to downgrade jscodeshift to 0.7.1 but it didn't help.

rndmerle avatar Mar 01 '22 12:03 rndmerle

@rndmerle Yeah I will have to enhance typings by declaring appropriate types/interfaces. For now feel free to ignore issues by specifying : any or as any wherever needed.

matrunchyk avatar Mar 01 '22 12:03 matrunchyk

It would be great if we can update this to use the new Vue 2.7 defineComponent instead. Which has much better type inference, however I don't think it supports mixins.

NikhilVerma avatar Jul 05 '22 10:07 NikhilVerma

This is exactly what it does for TS components keeping all the typings.

Btw, I have another script which migrates Vue2 to Vue3 script setup (mixin are also supported). I'll publish/share a repo if someone will be interested.

matrunchyk avatar Jul 05 '22 11:07 matrunchyk

@matrunchyk You are using the Vue.extend api. I am talking about defineComponent from Vue 2.7 (and 3) which has much better support for Typescript.

https://blog.vuejs.org/posts/vue-2-7-naruto.html

NikhilVerma avatar Jul 05 '22 11:07 NikhilVerma

Ah you're right @NikhilVerma. I think the argument of Vue.extend and defineCompontent is compatible, so after using the script you could just Find-Replace all mentions of Vue.extend to defineComponent as-is. That's basically what I did in my projects.

But I agree, we can replace it directly in the migration script itself.

matrunchyk avatar Jul 05 '22 11:07 matrunchyk

Hey @matrunchyk ,

Btw, I have another script which migrates Vue2 to Vue3 script setup (mixin are also supported). I'll publish/share a repo if someone will be interested.

I'd be interested in the Vue 3 script setup version even if only partially working since I have a few (read: hundreds) of components to convert, and any starting point is better than none. 😅 Would you mind sharing it with me?

smk avatar May 18 '23 09:05 smk

Hi @matrunchyk Can you pls let me know how to use this to convert vue2 class based code with TS to vue3 options api

Satakshi-ctrl avatar Mar 06 '24 05:03 Satakshi-ctrl