Skip to content

Commit ccf6b3b

Browse files
committed
fix(types): props type is incompatible with setup returned type
1 parent 2ffe3d5 commit ccf6b3b

File tree

3 files changed

+51
-30
lines changed

3 files changed

+51
-30
lines changed

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

+47-25
Original file line numberDiff line numberDiff line change
@@ -1346,22 +1346,20 @@ describe('function syntax w/ runtime props', () => {
13461346
<T extends string>(_props: { msg: T }) => {
13471347
return () => {}
13481348
},
1349-
{
1350-
props: {
1351-
msg: String
1349+
setup(props) {
1350+
expectType<SizeType>(props.size)
1351+
return {
1352+
size: 1
13521353
}
13531354
}
1354-
)
1355+
})
1356+
type CompInstance = InstanceType<typeof Comp>
13551357

1356-
// @ts-expect-error string prop names don't match
1357-
defineComponent(
1358-
(_props: { msg: string }) => {
1359-
return () => {}
1360-
},
1361-
{
1362-
props: ['bar']
1363-
}
1364-
)
1358+
const CompA = {} as CompInstance
1359+
expectType<ComponentPublicInstance>(CompA)
1360+
expectType<number>(CompA.size)
1361+
expectType<SizeType>(CompA.$props.size)
1362+
})
13651363

13661364
defineComponent(
13671365
(_props: { msg: string }) => {
@@ -1372,21 +1370,20 @@ describe('function syntax w/ runtime props', () => {
13721370
// @ts-expect-error prop type mismatch
13731371
msg: Number
13741372
}
1375-
}
1376-
)
1377-
1378-
// @ts-expect-error prop keys don't match
1379-
defineComponent(
1380-
(_props: { msg: string }, ctx) => {
1381-
return () => {}
13821373
},
1383-
{
1384-
props: {
1385-
msg: String,
1386-
bar: String
1374+
setup(props) {
1375+
expectType<SizeType>(props.size)
1376+
return {
1377+
size: 1
13871378
}
13881379
}
1389-
)
1380+
})
1381+
type CompInstance = InstanceType<typeof Comp>
1382+
1383+
const CompA = {} as CompInstance
1384+
expectType<ComponentPublicInstance>(CompA)
1385+
expectType<number>(CompA.size)
1386+
expectType<SizeType>(CompA.$props.size)
13901387
})
13911388

13921389
// check if defineComponent can be exported
@@ -1472,6 +1469,31 @@ describe('slots', () => {
14721469
expectType<Slots | undefined>(new comp2().$slots)
14731470
})
14741471

1472+
// #5885
1473+
describe('should work when props type is incompatible with setup returned type ', () => {
1474+
type SizeType = 'small' | 'big'
1475+
const Comp = defineComponent({
1476+
props: {
1477+
size: {
1478+
type: String as PropType<SizeType>,
1479+
required: true
1480+
}
1481+
},
1482+
setup(props) {
1483+
expectType<SizeType>(props.size)
1484+
return {
1485+
size: 1
1486+
}
1487+
}
1488+
})
1489+
type CompInstance = InstanceType<typeof Comp>
1490+
1491+
const CompA = {} as CompInstance
1492+
expectType<ComponentPublicInstance>(CompA)
1493+
expectType<number>(CompA.size)
1494+
expectType<SizeType>(CompA.$props.size)
1495+
})
1496+
14751497
import {
14761498
DefineComponent,
14771499
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<
@@ -228,7 +228,7 @@ export type ComponentPublicInstance<
228228
: (...args: any) => any,
229229
options?: WatchOptions
230230
): WatchStopHandle
231-
} & P &
231+
} & IfAny<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &
232232
ShallowUnwrapRef<B> &
233233
UnwrapNestedRefs<D> &
234234
ExtractComputedReturns<C> &

0 commit comments

Comments
 (0)