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

fix(types): fix shallowRef type error #9839

Merged
merged 5 commits into from
Dec 16, 2023

Conversation

Alfred-Skyblue
Copy link
Member

@Alfred-Skyblue Alfred-Skyblue commented Dec 15, 2023

In #7853, a function overload export function shallowRef<T>(value: MaybeRef<T>): Ref<T> | ShallowRef<T> was added. Due to MaybeRef<T> encompassing all types, this resulted in all instances of shallowRef being inferred as Ref<T> | ShallowRef<T>, instead of just ShallowRef<T>.

const res1 = shallowRef(1 as any) // Ref<any> | ShallowRef<any>
const res2 = shallowRef(1) // Ref<number> | ShallowRef<number>
const res3 = shallowRef(ref('foo')) //  Ref<string> | ShallowRef<string>
const res4 = shallowRef({} as MaybeRef<{ name: string }>) // Ref<{name: string}> | ShallowRef<{name: string}>
const res5 = shallowRef('' as string | number) //  Ref<string | number> | ShallowRef<string | number>
const res6 = shallowRef('' as Ref<string[]> | string | number) //  ShallowRef<string | number | Ref<string[]>>

In this PR, I have modified the function overload, resulting in the following types:

const res = shallowRef(1 as any) // ShallowRef<any>
const res1 = shallowRef(1) // ShallowRef<number>
const res2 = shallowRef(ref('foo')) // Ref<string>
const res3 = shallowRef({} as MaybeRef<{ name: string }>) // Ref<{name: string}> | ShallowRef<{name: string}>
const res4 = shallowRef('' as string | number) // ShallowRef<string | number>
const res5 = shallowRef('' as Ref<string[]> | string | number) // ShallowRef<number> | Ref<string[]> | ShallowRef<string>

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 87 kB 33.1 kB 29.9 kB
vue.global.prod.js 133 kB 50 kB 44.8 kB

Usages

Name Size Gzip Brotli
createApp 48.4 kB 19 kB 17.4 kB
createSSRApp 51.6 kB 20.3 kB 18.5 kB
defineCustomElement 50.7 kB 19.8 kB 18.1 kB
overall 61.7 kB 23.9 kB 21.8 kB

Copy link

codspeed-hq bot commented Dec 15, 2023

CodSpeed Performance Report

Merging #9839 will degrade performances by 42.4%

Comparing Alfred-Skyblue:fix/types/shallowRef (09d913b) with main (0a387df)

Summary

❌ 1 regressions
✅ 52 untouched benchmarks

⚠️ Please fix the performance issues or acknowledge them on CodSpeed.

Benchmarks breakdown

Benchmark main Alfred-Skyblue:fix/types/shallowRef Change
write reactive obj, read 1000 computeds 39.5 ms 68.6 ms -42.4%

@@ -201,11 +201,23 @@ if (refStatus.value === 'initial') {
expectType<IsAny<typeof a>>(false)
}

describe('shallowRef with generic', <T>() => {
const r = ref({}) as MaybeRef<T>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change does break the MaybeRef<T> case, but the correctness improvement is worth the tradeoff IMO.

@yyx990803 yyx990803 merged commit 9a57158 into vuejs:main Dec 16, 2023
12 of 13 checks passed
@Alfred-Skyblue Alfred-Skyblue deleted the fix/types/shallowRef branch December 16, 2023 06:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants