Skip to content

Commit

Permalink
fix(types): should unwrap tuple correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
hyf0 committed Oct 22, 2021
1 parent 5eb7263 commit 9909359
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 23 deletions.
13 changes: 5 additions & 8 deletions packages/reactivity/src/ref.ts
Expand Up @@ -255,13 +255,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>
Expand All @@ -277,7 +274,7 @@ export type UnwrapRefSimple<T> = T extends
| Ref
| RefUnwrapBailTypes[keyof RefUnwrapBailTypes]
? T
: T extends Array<any>
: T extends ReadonlyArray<any>
? { [K in keyof T]: UnwrapRefSimple<T[K]> }
: T extends object & { [ShallowReactiveMarker]?: never }
? {
Expand Down
48 changes: 33 additions & 15 deletions test-dts/reactivity.test-d.ts
@@ -1,15 +1,33 @@
import { ref, readonly, describe, expectError, expectType, Ref } from './index'

describe('should support DeepReadonly', () => {
const r = readonly({ obj: { k: 'v' } })
// @ts-expect-error
expectError((r.obj = {}))
// @ts-expect-error
expectError((r.obj.k = 'x'))
})

// #4180
describe('readonly ref', () => {
const r = readonly(ref({ count: 1 }))
expectType<Ref>(r)
})
import {
ref,
readonly,
describe,
expectError,
expectType,
Ref,
reactive
} from './index'

describe('should support DeepReadonly', () => {
const r = readonly({ obj: { k: 'v' } })
// @ts-expect-error
expectError((r.obj = {}))
// @ts-expect-error
expectError((r.obj.k = 'x'))
})

// #4180
describe('readonly ref', () => {
const r = readonly(ref({ count: 1 }))
expectType<Ref>(r)
})

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])
})

0 comments on commit 9909359

Please sign in to comment.