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(reactivity): shallowRef type fix #7853

Merged
merged 7 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/dts-test/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ import {
MaybeRef,
MaybeRefOrGetter,
ComputedRef,
computed
computed,
ShallowRef
} from 'vue'
import { expectType, describe } from './utils'
import { expectType, describe, IsUnion } from './utils'

function plainType(arg: number | Ref<number>) {
// ref coercing
Expand Down Expand Up @@ -174,6 +175,27 @@ if (refStatus.value === 'initial') {
refStatus.value = 'invalidating'
}

{
const shallow = shallowRef(1)
expectType<Ref<number>>(shallow)
expectType<ShallowRef<number>>(shallow)
}

{
//#7852
type Steps = { step: '1' } | { step: '2' }
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)

expectType<IsUnion<typeof shallowUnionGenParam>>(false)
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
}

describe('shallowRef with generic', <T>() => {
const r = ref({}) as MaybeRef<T>
expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
})

// proxyRefs: should return `reactive` directly
const r1 = reactive({
k: 'v'
Expand Down
16 changes: 15 additions & 1 deletion packages/dts-test/watch.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ref, computed, watch, defineComponent } from 'vue'
import { ref, computed, watch, defineComponent, shallowRef } from 'vue'
import { expectType } from './utils'

const source = ref('foo')
Expand Down Expand Up @@ -92,3 +92,17 @@ defineComponent({
)
}
})

{
//#7852
type Steps = { step: '1' } | { step: '2' }
const shallowUnionGenParam = shallowRef<Steps>({ step: '1' })
const shallowUnionAsCast = shallowRef({ step: '1' } as Steps)

watch(shallowUnionGenParam, value => {
expectType<Steps>(value)
})
watch(shallowUnionAsCast, value => {
expectType<Steps>(value)
})
}
5 changes: 2 additions & 3 deletions packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ export type ShallowRef<T = any> = Ref<T> & { [ShallowRefMarker]?: true }
* @param value - The "inner value" for the shallow ref.
* @see {@link https://vuejs.org/api/reactivity-advanced.html#shallowref}
*/
export function shallowRef<T extends object>(
value: T
): T extends Ref ? T : ShallowRef<T>
export function shallowRef<T>(value: MaybeRef<T>): Ref<T> | ShallowRef<T>
export function shallowRef<T extends Ref>(value: T): T
export function shallowRef<T>(value: T): ShallowRef<T>
export function shallowRef<T = any>(): ShallowRef<T | undefined>
export function shallowRef(value?: unknown) {
Expand Down