Skip to content

Commit 0e1e8f9

Browse files
authoredNov 9, 2023
fix(types): fix instance type when props type is incompatible with setup returned type (#7338)
close #5885
1 parent 657476d commit 0e1e8f9

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed
 

‎packages/dts-test/defineComponent.test-d.tsx

+25
Original file line numberDiff line numberDiff line change
@@ -1472,6 +1472,31 @@ describe('slots', () => {
14721472
expectType<Slots | undefined>(new comp2().$slots)
14731473
})
14741474

1475+
// #5885
1476+
describe('should work when props type is incompatible with setup returned type ', () => {
1477+
type SizeType = 'small' | 'big'
1478+
const Comp = defineComponent({
1479+
props: {
1480+
size: {
1481+
type: String as PropType<SizeType>,
1482+
required: true
1483+
}
1484+
},
1485+
setup(props) {
1486+
expectType<SizeType>(props.size)
1487+
return {
1488+
size: 1
1489+
}
1490+
}
1491+
})
1492+
type CompInstance = InstanceType<typeof Comp>
1493+
1494+
const CompA = {} as CompInstance
1495+
expectType<ComponentPublicInstance>(CompA)
1496+
expectType<number>(CompA.size)
1497+
expectType<SizeType>(CompA.$props.size)
1498+
})
1499+
14751500
import {
14761501
DefineComponent,
14771502
ComponentOptionsMixin,

‎packages/runtime-core/src/apiDefineComponent.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ export type DefineComponent<
7070
true,
7171
{},
7272
S
73-
> &
74-
Props
73+
>
7574
> &
7675
ComponentOptionsBase<
7776
Props,

‎packages/runtime-core/src/componentPublicInstance.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ import {
1515
isString,
1616
isFunction,
1717
UnionToIntersection,
18-
Prettify
18+
Prettify,
19+
IfAny
1920
} from '@vue/shared'
2021
import {
2122
toRaw,
@@ -187,7 +188,6 @@ export type CreateComponentPublicInstance<
187188
I,
188189
S
189190
>
190-
191191
// public properties exposed on the proxy, which is used as the render context
192192
// in templates (as `this` in the render option)
193193
export type ComponentPublicInstance<
@@ -226,7 +226,7 @@ export type ComponentPublicInstance<
226226
: (...args: any) => any,
227227
options?: WatchOptions
228228
): WatchStopHandle
229-
} & P &
229+
} & IfAny<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &
230230
ShallowUnwrapRef<B> &
231231
UnwrapNestedRefs<D> &
232232
ExtractComputedReturns<C> &

0 commit comments

Comments
 (0)
Please sign in to comment.