Skip to content

Commit

Permalink
types(defineComponent): fix unwrap when returning Ref<T>|undefined
Browse files Browse the repository at this point in the history
…from `setup` (#4100)
  • Loading branch information
pikax authored Jul 15, 2021
1 parent e508ee0 commit f6a5f09
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 3 deletions.
6 changes: 5 additions & 1 deletion packages/reactivity/src/ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,11 @@ type BaseTypes = string | number | boolean
export interface RefUnwrapBailTypes {}

export type ShallowUnwrapRef<T> = {
[K in keyof T]: T[K] extends Ref<infer V> ? V : T[K]
[K in keyof T]: T[K] extends Ref<infer V>
? V
: T[K] extends Ref<infer V> | undefined // if `V` is `unknown` that means it does not extend `Ref` and is undefined
? unknown extends V ? undefined : V | undefined
: T[K]
}

export type UnwrapRef<T> = T extends Ref<infer V>
Expand Down
6 changes: 4 additions & 2 deletions test-dts/component.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ describe('object props', () => {
setupC: {
a: ref(2)
},
setupD: undefined as Ref<number> | undefined,
setupProps: props
}
}
Expand Down Expand Up @@ -190,7 +191,7 @@ describe('object props', () => {
expectType<Number>(rawBindings.setupA)
expectType<Ref<Number>>(rawBindings.setupB)
expectType<Ref<Number>>(rawBindings.setupC.a)
expectType<Number>(rawBindings.setupA)
expectType<Ref<Number> | undefined>(rawBindings.setupD)

// raw bindings props
expectType<ExpectedProps['a']>(rawBindings.setupProps.a)
Expand All @@ -215,7 +216,7 @@ describe('object props', () => {
expectType<Number>(setup.setupA)
expectType<Number>(setup.setupB)
expectType<Ref<Number>>(setup.setupC.a)
expectType<Number>(setup.setupA)
expectType<number | undefined>(setup.setupD)

// raw bindings props
expectType<ExpectedProps['a']>(setup.setupProps.a)
Expand All @@ -239,6 +240,7 @@ describe('object props', () => {
// instance
const instance = new MyComponent()
expectType<number>(instance.setupA)
expectType<number | undefined>(instance.setupD)
// @ts-expect-error
instance.notExist
})
Expand Down

0 comments on commit f6a5f09

Please sign in to comment.