-
Notifications
You must be signed in to change notification settings - Fork 12.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
Regression in Vue typings in 3.4.x #30854
Comments
Minimal repro here: vuejs/vue#9873 (comment) type TypedDef<Data, Computed> =
ComponentOptions<Data, Computed> &
ThisType<Data & Computed>
type DataDef<Data> = () => Data
export interface ComponentOptions<Data, Computed> {
data?: DataDef<Data>
computed?: Accessors<Computed>
}
export type Accessors<T> = {
[K in keyof T]: () => T[K]
}
declare function component<Data, Computed>(def: TypedDef<Data, Computed>): void;
component({
data() {
return {
foo: 23
}
},
computed: {
bar() {
return this.foo + 1 // comment out the return solves the problem
}
}
}) |
Adding the return type to |
I'm surprised this ever worked without a type annotation. It certainly was not intended to. In the examples above, when a method without a return type annotation references I know that we've gotten better at being consistent in the face of circularities in 3.4, in the sense that we consistently produce the same types for entities during all phases of inference and in quick info. I'm not exactly sure why the example worked in 3.3, and it would take some debugging to find out. But either way, 3.4 behaves as intended and the solution is to add return type annotations to methods that reference |
The solution does work for this case but if the computed property would reference a prop the error is not solvable by adding a return type. Example:
|
computed's return type is required for ts? why? I don't want to add the return type... |
Edit: Nevermind, there is such a simple way to do it, and it's to extend Vue so that the properties get correctly "linked" to the component instance itself... I just had not found the answer before writing this, so I guess just forget what I've said lol |
TypeScript Version: 3.4.1, 3.4.2, 3.4.3, 3.5.0-dev.20190410
Search Terms: Vue 3.4 typing
Code
It's not clear to me if this is a problem with Typescript or with Vue's types, but it started happening in Typescript 3.4 with no change to Vue.
I have a repo that reproduces the problem here: https://github.com/mrozekma/typescript-vue-problem. This was created using Vue CLI with just Typescript enabled, followed by upgrading to Typescript 3.4 and adding the following to the default export in
src/App.vue
:Expected behavior:
Should build without errors.
Actual behavior:
This builds fine with 3.3.3, and according to Visual Studio Code it's valid (even version 1.33.0 with bundled Typescript 3.4.1), but if I try to build with any Typescript 3.4.x I get:
The type of
this
on line 19 is unexpectedly sparse. The second template argument should be the return type of thedata
function, and looks correct in Code's tooltip of that line:Playground Link:
Related Issues:
Possibly related to #30442, as it mentions a regression with intersection types, which
ComputedVueInstance
is.The text was updated successfully, but these errors were encountered: