Skip to content

Commit d538fd2

Browse files
committed
fix(types): props type is incompatible with setup returned type
1 parent 6e8b6b3 commit d538fd2

File tree

3 files changed

+35
-12
lines changed

3 files changed

+35
-12
lines changed

packages/runtime-core/src/apiDefineComponent.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ export type DefineComponent<
6262
PP & Props,
6363
Defaults,
6464
true
65-
> &
66-
Props
65+
>
6766
> &
6867
ComponentOptionsBase<
6968
Props,

packages/runtime-core/src/componentPublicInstance.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import {
1414
extend,
1515
isString,
1616
isFunction,
17-
UnionToIntersection
17+
UnionToIntersection,
18+
IfAny
1819
} from '@vue/shared'
1920
import {
2021
toRaw,
@@ -167,7 +168,6 @@ export type CreateComponentPublicInstance<
167168
ComponentOptionsBase<P, B, D, C, M, Mixin, Extends, E, string, Defaults>,
168169
I
169170
>
170-
171171
// public properties exposed on the proxy, which is used as the render context
172172
// in templates (as `this` in the render option)
173173
export type ComponentPublicInstance<
@@ -205,7 +205,7 @@ export type ComponentPublicInstance<
205205
: (...args: any) => any,
206206
options?: WatchOptions
207207
): WatchStopHandle
208-
} & P &
208+
} & IfAny<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &
209209
ShallowUnwrapRef<B> &
210210
UnwrapNestedRefs<D> &
211211
ExtractComputedReturns<C> &

test-dts/defineComponent.test-d.tsx

+31-7
Original file line numberDiff line numberDiff line change
@@ -1041,13 +1041,13 @@ describe('inject', () => {
10411041
},
10421042
inject: {
10431043
foo: 'foo',
1044-
bar: 'bar',
1044+
bar: 'bar'
10451045
},
10461046
created() {
10471047
expectType<unknown>(this.foo)
10481048
expectType<unknown>(this.bar)
10491049
// @ts-expect-error
1050-
expectError(this.foobar = 1)
1050+
expectError((this.foobar = 1))
10511051
}
10521052
})
10531053

@@ -1059,7 +1059,7 @@ describe('inject', () => {
10591059
expectType<unknown>(this.foo)
10601060
expectType<unknown>(this.bar)
10611061
// @ts-expect-error
1062-
expectError(this.foobar = 1)
1062+
expectError((this.foobar = 1))
10631063
}
10641064
})
10651065

@@ -1073,13 +1073,13 @@ describe('inject', () => {
10731073
bar: {
10741074
from: 'pbar',
10751075
default: 'bar'
1076-
},
1076+
}
10771077
},
10781078
created() {
10791079
expectType<unknown>(this.foo)
10801080
expectType<unknown>(this.bar)
10811081
// @ts-expect-error
1082-
expectError(this.foobar = 1)
1082+
expectError((this.foobar = 1))
10831083
}
10841084
})
10851085

@@ -1088,9 +1088,9 @@ describe('inject', () => {
10881088
props: ['a', 'b'],
10891089
created() {
10901090
// @ts-expect-error
1091-
expectError(this.foo = 1)
1091+
expectError((this.foo = 1))
10921092
// @ts-expect-error
1093-
expectError(this.bar = 1)
1093+
expectError((this.bar = 1))
10941094
}
10951095
})
10961096
})
@@ -1257,6 +1257,30 @@ describe('prop starting with `on*` is broken', () => {
12571257
})
12581258
})
12591259

1260+
describe('should work when props type is incompatible with setup returned type ', () => {
1261+
type SizeType = 'small' | 'big'
1262+
const Comp = defineComponent({
1263+
props: {
1264+
size: {
1265+
type: String as PropType<SizeType>,
1266+
required: true
1267+
}
1268+
},
1269+
setup(props) {
1270+
expectType<SizeType>(props.size)
1271+
return {
1272+
size: 1
1273+
}
1274+
}
1275+
})
1276+
type CompInstance = InstanceType<typeof Comp>
1277+
1278+
const CompA = {} as CompInstance
1279+
expectType<ComponentPublicInstance>(CompA)
1280+
expectType<number>(CompA.size)
1281+
expectType<SizeType>(CompA.$props.size)
1282+
})
1283+
12601284
// check if defineComponent can be exported
12611285
export default {
12621286
// function components

0 commit comments

Comments
 (0)