Skip to content

Commit cdac121

Browse files
fix(types): ref() return type should not be any when initial value is any (#9768)
1 parent b4ac0e6 commit cdac121

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

packages/dts-test/ref.test-d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
computed,
1919
ShallowRef
2020
} from 'vue'
21-
import { expectType, describe, IsUnion } from './utils'
21+
import { expectType, describe, IsUnion, IsAny } from './utils'
2222

2323
function plainType(arg: number | Ref<number>) {
2424
// ref coercing
@@ -79,6 +79,10 @@ function plainType(arg: number | Ref<number>) {
7979
// should still unwrap in objects nested in arrays
8080
const arr2 = ref([{ a: ref(1) }]).value
8181
expectType<number>(arr2[0].a)
82+
83+
// any value should return Ref<any>, not any
84+
const a = ref(1 as any)
85+
expectType<IsAny<typeof a>>(false)
8286
}
8387

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

198+
{
199+
// any value should return Ref<any>, not any
200+
const a = shallowRef(1 as any)
201+
expectType<IsAny<typeof a>>(false)
202+
}
203+
194204
describe('shallowRef with generic', <T>() => {
195205
const r = ref({}) as MaybeRef<T>
196206
expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))

packages/reactivity/src/ref.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ export function isRef(r: any): r is Ref {
8787
* @param value - The object to wrap in the ref.
8888
* @see {@link https://vuejs.org/api/reactivity-core.html#ref}
8989
*/
90-
export function ref<T extends Ref>(value: T): T
9190
export function ref<T>(value: T): Ref<UnwrapRef<T>>
9291
export function ref<T = any>(): Ref<T | undefined>
9392
export function ref(value?: unknown) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy