Skip to content

Commit

Permalink
fix(types): ref() return type should not be any when initial value is…
Browse files Browse the repository at this point in the history
… any (#9768)
  • Loading branch information
Alfred-Skyblue authored Dec 7, 2023
1 parent b4ac0e6 commit cdac121
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
12 changes: 11 additions & 1 deletion packages/dts-test/ref.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
computed,
ShallowRef
} from 'vue'
import { expectType, describe, IsUnion } from './utils'
import { expectType, describe, IsUnion, IsAny } from './utils'

function plainType(arg: number | Ref<number>) {
// ref coercing
Expand Down Expand Up @@ -79,6 +79,10 @@ function plainType(arg: number | Ref<number>) {
// should still unwrap in objects nested in arrays
const arr2 = ref([{ a: ref(1) }]).value
expectType<number>(arr2[0].a)

// any value should return Ref<any>, not any
const a = ref(1 as any)
expectType<IsAny<typeof a>>(false)
}

plainType(1)
Expand Down Expand Up @@ -191,6 +195,12 @@ if (refStatus.value === 'initial') {
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
}

{
// any value should return Ref<any>, not any
const a = shallowRef(1 as any)
expectType<IsAny<typeof a>>(false)
}

describe('shallowRef with generic', <T>() => {
const r = ref({}) as MaybeRef<T>
expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
Expand Down
1 change: 0 additions & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ export function isRef(r: any): r is Ref {
* @param value - The object to wrap in the ref.
* @see {@link https://vuejs.org/api/reactivity-core.html#ref}
*/
export function ref<T extends Ref>(value: T): T
export function ref<T>(value: T): Ref<UnwrapRef<T>>
export function ref<T = any>(): Ref<T | undefined>
export function ref(value?: unknown) {
Expand Down

0 comments on commit cdac121

Please sign in to comment.