-
Notifications
You must be signed in to change notification settings - Fork 4.6k
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
Clarify V3 Migration strategy for TS Vue.extend (#2) #1502
Comments
I don't know the position of Vue team, but, as a user, if I aimed for optimal TypeScript support:
|
I get that you can use for all of that composition API. We indeed want to use that in future. But for large codebases, that's not an easy path to rewrite suddenly everything in one go using composition API. Especially if it was supported in Vue2 and as I understand Vue3 still fully supports Options API. So I'm hoping Typescript support for Options API can also kept in place. Otherwise it juts got crippled for TS users ... keeping Options API in place with removed TS support. So vue2 TS users will be forced to rewrite all at once or nothing. |
You don't need to "rewrite suddenly everything in one go". Composition API can be used inside Options API. You can migrate mixin by mixin, consumer by consumer, often without having to touch the rest of the component (if the mixins didn't have implicit dependencies in non-mixin code). But, yes, I don't know if there's an easy and type-safe migration path for TypeScript users that want to keep using local mixins. |
Let's assume vue2 is swapped to vue3 and we deal with local mixins in local components (!no global vue instance augmentation!).
Am I missing something how you can do this gradually without rewriting everything in one go if TS relied on Vue.extend? Happy to be guided here, really :-) |
Anyway, after further research, it seems that The full suite of |
Resolves to unknown for me, even if all defined with |
So let's approach this gradually and identify how far we can get: ~~Concerning a gradual approach, you could use the Vue 2 composition API plugin to be able to use Edit: Unfortunately, the plugin's version of
|
Thanks, already using composition plugin and trying to have new work vue3 forward compatible. Though these are approaches how to do the rewrites to composition API, not to keep types for existing Options API which is still supported, but weirdly types are not. So once vue2 to vue3 is swapped, I was more wondering if they could be provided through |
@pikax do you have any bright ideas on how we could enable that? |
@LinusBorg maybe providing an Vue3 provides superior typescript support than vue2, with full support for Options API. @lukashroch do you mind providing a concrete example of what is not supported? Composition-API is preferred instead of Mixins going forward. Before I misunderstand your question do you mind explaining (ideally with a repo code/link) what you cannot achieve in vue3 but you're able to do it on vue2?
Personally I would find&replace, replacing
Do you mind explaining a bit better? That is something that I would try to avoid, if you need mixins, just import them in the component. defineComponent({
mixins: [ someMixin ]
// ....
})
Not sure what you mean here, if the
They are replaced with the
This should work in vue3 for sure! Can you provide an example? |
@pikax let me try and give you a quick recap of the problem: As we know in Vue 2 Options API, there are a bunch of things that are not straightforward to get type inference for when usign
OP found a way that works for them by extending the But that way is not replicable in Vue 3's So the main question is: is there a way to extend an individual component's |
Thank you @LinusBorg Now I understand the issue that OP is facing. Extending
declare function mapGetters(o: any): any
defineComponent({
computed: {
// NOTE we need to cast it to be a function, just like a computed is a function
...(mapGetters(['foo', 'bar']) as { foo: ()=> number; bar: ()=>string })
},
methods: {
test(){
this.foo.toExponential()
// @ts-expect-error
this.bar = this.foo
}
}
}) |
@pikax, I'm not questioning superiority of the vue3 & ts support. It works like a charm in vue3. And I agree this The issue is that Options API & TS works with So if you have such interface, and various types with different migration strategies, you have to resolve them at once, since you removed the whole thing. And if you have codebase with hundreds of components and mixins, entangled, you suddenly have to migrate large chunk of code at once. In that interface you can e.g. have:
Each of these can be resolved and will be better once it's done in composition API, I'm not arguing about that. But the problem is that if you take the |
That's correct, they won't work together well. My recommendation is moving everything to If you migrating, the first thing is to check the migration, after you are using vue3 (without typechecking yet), the next step is to replace the
If having typed
Lifecycle are a bit tricker to alter, but you can do something like this (I don't recommend that, but it might be an option), there's no need to use
I guess you won't have this issue if you don't port to
Direct casting always comes back to bit you, I can provide help on particular scenarios and how to possibly type them correctly, each codebase is different. |
@pikax, @LinusBorg thank you, really helpful to have these answers! Does the TS inference work for |
@lukashroch yes only on Vue3 |
@pikax thanks for confirming! |
Sorry for re-opening, but how does this apply for single components & vue3? This is OK for augmenting the global vue (which is already in vue2), but not for per-component case in vue3. The original example is for vue2 & single component.
https://vuejs.org/api/utility-types.html#componentcustomproperties
Originally posted by @yyx990803 in #1446 (comment)
The text was updated successfully, but these errors were encountered: