mirror of https://github.com/vuejs/core.git
fix(types): ref() return type should not be any when initial value is any (#9768)
This commit is contained in:
parent
b4ac0e6ac1
commit
cdac12161e
|
@ -18,7 +18,7 @@ import {
|
||||||
computed,
|
computed,
|
||||||
ShallowRef
|
ShallowRef
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
import { expectType, describe, IsUnion } from './utils'
|
import { expectType, describe, IsUnion, IsAny } from './utils'
|
||||||
|
|
||||||
function plainType(arg: number | Ref<number>) {
|
function plainType(arg: number | Ref<number>) {
|
||||||
// ref coercing
|
// ref coercing
|
||||||
|
@ -79,6 +79,10 @@ function plainType(arg: number | Ref<number>) {
|
||||||
// should still unwrap in objects nested in arrays
|
// should still unwrap in objects nested in arrays
|
||||||
const arr2 = ref([{ a: ref(1) }]).value
|
const arr2 = ref([{ a: ref(1) }]).value
|
||||||
expectType<number>(arr2[0].a)
|
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)
|
plainType(1)
|
||||||
|
@ -191,6 +195,12 @@ if (refStatus.value === 'initial') {
|
||||||
expectType<IsUnion<typeof shallowUnionAsCast>>(false)
|
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>() => {
|
describe('shallowRef with generic', <T>() => {
|
||||||
const r = ref({}) as MaybeRef<T>
|
const r = ref({}) as MaybeRef<T>
|
||||||
expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
|
expectType<ShallowRef<T> | Ref<T>>(shallowRef(r))
|
||||||
|
|
|
@ -87,7 +87,6 @@ export function isRef(r: any): r is Ref {
|
||||||
* @param value - The object to wrap in the ref.
|
* @param value - The object to wrap in the ref.
|
||||||
* @see {@link https://vuejs.org/api/reactivity-core.html#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>(value: T): Ref<UnwrapRef<T>>
|
||||||
export function ref<T = any>(): Ref<T | undefined>
|
export function ref<T = any>(): Ref<T | undefined>
|
||||||
export function ref(value?: unknown) {
|
export function ref(value?: unknown) {
|
||||||
|
|
Loading…
Reference in New Issue