Skip to content

Commit 85821fd

Browse files
committed
fix(types): props type is incompatible with setup returned type
1 parent a0e7dc3 commit 85821fd

File tree

3 files changed

+33
-10
lines changed

3 files changed

+33
-10
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,7 +1040,7 @@ describe('inject', () => {
10401040
expectType<unknown>(this.foo)
10411041
expectType<unknown>(this.bar)
10421042
// @ts-expect-error
1043-
this.foobar = 1
1043+
expectError((this.foobar = 1))
10441044
}
10451045
})
10461046

@@ -1052,7 +1052,7 @@ describe('inject', () => {
10521052
expectType<unknown>(this.foo)
10531053
expectType<unknown>(this.bar)
10541054
// @ts-expect-error
1055-
this.foobar = 1
1055+
expectError((this.foobar = 1))
10561056
}
10571057
})
10581058

@@ -1072,7 +1072,7 @@ describe('inject', () => {
10721072
expectType<unknown>(this.foo)
10731073
expectType<unknown>(this.bar)
10741074
// @ts-expect-error
1075-
this.foobar = 1
1075+
expectError((this.foobar = 1))
10761076
}
10771077
})
10781078

@@ -1081,9 +1081,9 @@ describe('inject', () => {
10811081
props: ['a', 'b'],
10821082
created() {
10831083
// @ts-expect-error
1084-
this.foo = 1
1084+
expectError((this.foo = 1))
10851085
// @ts-expect-error
1086-
this.bar = 1
1086+
expectError((this.bar = 1))
10871087
}
10881088
})
10891089
})
@@ -1250,6 +1250,30 @@ describe('prop starting with `on*` is broken', () => {
12501250
})
12511251
})
12521252

1253+
describe('should work when props type is incompatible with setup returned type ', () => {
1254+
type SizeType = 'small' | 'big'
1255+
const Comp = defineComponent({
1256+
props: {
1257+
size: {
1258+
type: String as PropType<SizeType>,
1259+
required: true
1260+
}
1261+
},
1262+
setup(props) {
1263+
expectType<SizeType>(props.size)
1264+
return {
1265+
size: 1
1266+
}
1267+
}
1268+
})
1269+
type CompInstance = InstanceType<typeof Comp>
1270+
1271+
const CompA = {} as CompInstance
1272+
expectType<ComponentPublicInstance>(CompA)
1273+
expectType<number>(CompA.size)
1274+
expectType<SizeType>(CompA.$props.size)
1275+
})
1276+
12531277
// check if defineComponent can be exported
12541278
export default {
12551279
// function components

packages/runtime-core/src/apiDefineComponent.ts

Lines changed: 1 addition & 2 deletions
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

Lines changed: 3 additions & 3 deletions
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> &

0 commit comments

Comments
 (0)