fix(types): should unwrap tuple correctly (#3820)

fix #3819
This commit is contained in:
HeYunfei 2022-10-26 16:27:42 +08:00 committed by GitHub
parent 09bb3e996e
commit e816812f10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 70 deletions

View File

@ -280,13 +280,10 @@ export interface RefUnwrapBailTypes {}
export type ShallowUnwrapRef<T> = {
[K in keyof T]: T[K] extends Ref<infer V>
? V
: // if `V` is `unknown` that means it does not extend `Ref` and is undefined
T[K] extends Ref<infer V> | undefined
? unknown extends V
? undefined
: V | undefined
: T[K]
? V // if `V` is `unknown` that means it does not extend `Ref` and is undefined
: T[K] extends Ref<infer V> | undefined
? unknown extends V ? undefined : V | undefined
: T[K]
}
export type UnwrapRef<T> = T extends ShallowRef<infer V>
@ -303,7 +300,7 @@ export type UnwrapRefSimple<T> = T extends
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
| { [RawSymbol]?: true }
? T
: T extends Array<any>
: T extends ReadonlyArray<any>
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
: T extends object & { [ShallowReactiveMarker]?: never }
? {

View File

@ -60,3 +60,14 @@ describe('shallowReadonly ref unwrap', () => {
expectType<Ref>(r.count.n)
r.count.n.value = 123
})
// #3819
describe('should unwrap tuple correctly', () => {
const readonlyTuple = [ref(0)] as const
const reactiveReadonlyTuple = reactive(readonlyTuple)
expectType<Ref<number>>(reactiveReadonlyTuple[0])
const tuple: [Ref<number>] = [ref(0)]
const reactiveTuple = reactive(tuple)
expectType<Ref<number>>(reactiveTuple[0])
})