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

shallowRef's return type should match ref's #7852

Closed
simlevesque opened this issue Mar 7, 2023 · 2 comments · Fixed by #7853
Closed

shallowRef's return type should match ref's #7852

simlevesque opened this issue Mar 7, 2023 · 2 comments · Fixed by #7853

Comments

@simlevesque
Copy link
Contributor

simlevesque commented Mar 7, 2023

Vue version

3.2.47

Link to minimal reproduction

New reproduction with runtime error: codesandbox.io

typescriptlang.org

Steps to reproduce

  • Use shallowRef with a generic type params that's a union : shallowRef<{a:1} | {b:2}>({a:1})
  • Or use shallowRef and specify argument's type with as: shallowRef({a:1} as {a:1} | {b:2})

What is expected?

The return type should be ShallowRef<{a:1} | {b:2}>.

What is actually happening?

The return type is ShallowRef<{a:1}> | ShallowRef<{b:2}>

System Info

System:
    OS: Linux 6.1 Ubuntu 22.10 22.10 (Kinetic Kudu)
    CPU: (16) x64 AMD Ryzen 9 5900HX with Radeon Graphics
    Memory: 10.10 GB / 30.75 GB
    Container: Yes
    Shell: 3.5.1 - /usr/bin/fish
  Binaries:
    Node: 14.20.0 - ~/.local/share/nvm/v14.20.0/bin/node
    Yarn: 1.22.19 - ~/.local/share/nvm/v14.20.0/bin/yarn
    npm: 8.17.0 - ~/.local/share/nvm/v14.20.0/bin/npm
  Browsers:
    Chrome: 110.0.5481.177
    Firefox: 110.0.1

Any additional comments?

This breaks Vue's watch function. The first argument of watch's callback becomes a Ref instead of the value, which will cause a runtime error because Property 'value' does not exist on type '{ a: 1; } | { b: 2; }'

The reproduction link provides a good example of this. If you have any questions just ask me.

I've fixed the issue on my fork and the CI actions completed successfully.

You need to change
function shallowRef<T extends object>(value: T): T extends Ref ? T : ShallowRef<T>;

to
function shallowRef<T extends object>(value: T): [T] extends [Ref] ? T : ShallowRef<T>;

This matches ref's type:
function ref<T extends object>(value: T): [T] extends [Ref] ? T : Ref<UnwrapRef<T>>;

I think this bug is present in other parts of the code but I'm not sure right now.

@simlevesque
Copy link
Contributor Author

This error is also present in Vue 2.7.14: https://github.com/vuejs/vue/blob/main/src/v3/reactivity/ref.ts#L58

@simlevesque
Copy link
Contributor Author

I've added a better reproduction of the error to show it will lead to runtime error.

simlevesque added a commit to simlevesque/core that referenced this issue Mar 8, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants