Skip to content
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

Type not preserved with array ref since 3.2.30 #5367

Closed
JoostKersjes opened this issue Feb 7, 2022 · 6 comments
Closed

Type not preserved with array ref since 3.2.30 #5367

JoostKersjes opened this issue Feb 7, 2022 · 6 comments
Labels
🐞 bug Something isn't working scope: types

Comments

@JoostKersjes
Copy link

JoostKersjes commented Feb 7, 2022

Version

3.2.30

Reproduction link

sfc.vuejs.org/

Steps to reproduce

In the following block of Typescript code, look at the type of the list variable.

import { ref } from "vue";
type DropdownOption = { value: string; name: string };
const list = ref<DropdownOption[]>([]);

What is expected?

The return type of ref<DropdownOption[]>([]) to be Ref<DropdownOption[]>.

What is actually happening?

The return type of ref<DropdownOption[]>([]) is currently Ref<{value: string, name: string}[]>.

@JoostKersjes
Copy link
Author

I could be off-base here, but I think the problem statement above is the issue related to this Typescript error.

error TS2322: Type 'HTMLLIElement' is not assignable to type '{ type: string; value: number; addEventListener: { <K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLLIElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | ... 1 more ... | undefined): void; (type: string, listener: EventListenerOrEventListenerObject, options?: boolean | ... 1 more ... ...'.
  Types of property 'offsetParent' are incompatible.

That error does not appear in vue<=3.2.29. This is the code producing the error:

const optionElementRefs = ref<HTMLLIElement[]>([]);

// ...

const setOptionElementRef = (element: HTMLLIElement, index: number) => {
  optionElementRefs.value[index] = element;
};

@LinusBorg
Copy link
Member

That error does not appear in vue<=3.2.29. This is the code producing the error:

I can reproduce that second example with 3.2.25 at least, also 3.2.30 doesn't contain any changes that could be related.

@LinusBorg LinusBorg added 🐞 bug Something isn't working scope: types labels Feb 8, 2022
@LinusBorg
Copy link
Member

LinusBorg commented Feb 8, 2022

related, I think: #4807

@CipherdevNL
Copy link

In a project I'm working on the same issue occurred during an upgrade of only the yarn.lock file. Did some research and apparently if the version of @vue/reactivity mismatches with the pinned version of vue in the package.json it will produce this issue.

For the typescript type checking we're using vue-tsc and it seems like they have an indirect dependency on @vue/[email protected], thus making it possible for a mismatch upgrade in the yarn.lock file.

After bumping vue in the package.json to the same version it resolves the issue. So it seems like typescript get confused somehow when @vue/reactivity is not matching the version of other core vue packages.

Don't know if an investigation is really needed to know why a mismatch in versions result in this error, because it's supposed the version of core vue packages must be the same. But it might worth looking into if it's allowed that vue-tsc can result in such mismatch of core packages in the yarn.lock. Especially when vue-tsc is named on the vue site.

@JoostKersjes
Copy link
Author

Can confirm this issue was due to a mismatch, closing, thanks!

@AIREM-Richard
Copy link

AIREM-Richard commented May 7, 2022

I found the problem too, I have two questions:

  1. Why a version mismatch occured?
  2. Why the mismatch cause this problem?

If anyone have got the answer?

@github-actions github-actions bot locked and limited conversation to collaborators Sep 26, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
🐞 bug Something isn't working scope: types
Projects
None yet
Development

No branches or pull requests

4 participants