From a8637ff4a715f2d9e5afeffc8cd7ed57ab40484c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90=20Kevin=20Deng?= Date: Tue, 28 Mar 2023 15:40:03 +0800 Subject: [PATCH] fix(compiler-sfc): infer TSIntersectionType in defineProps (#7394) --- .../__tests__/__snapshots__/compileScript.spec.ts.snap | 1 + packages/compiler-sfc/__tests__/compileScript.spec.ts | 3 +++ packages/compiler-sfc/src/compileScript.ts | 3 +-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 05e5e4fd1fd..9018df65a18 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1643,6 +1643,7 @@ export default /*#__PURE__*/_defineComponent({ literalUnionNumber: { type: Number, required: true }, literalUnionMixed: { type: [String, Number, Boolean], required: true }, intersection: { type: Object, required: true }, + intersection2: { type: String, required: true }, foo: { type: [Function, null], required: true } }, setup(__props: any, { expose }) { diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 9c3c4a70273..10eeea01674 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -972,6 +972,7 @@ const emit = defineEmits(['a', 'b']) literalUnionNumber: 1 | 2 | 3 | 4 | 5 literalUnionMixed: 'foo' | 1 | boolean intersection: Test & {} + intersection2: 'foo' & ('foo' | 'bar') foo: ((item: any) => boolean) | null }>() `) @@ -1014,6 +1015,7 @@ const emit = defineEmits(['a', 'b']) `literalUnionMixed: { type: [String, Number, Boolean], required: true }` ) expect(content).toMatch(`intersection: { type: Object, required: true }`) + expect(content).toMatch(`intersection2: { type: String, required: true }`) expect(content).toMatch(`foo: { type: [Function, null], required: true }`) expect(bindings).toStrictEqual({ string: BindingTypes.PROPS, @@ -1044,6 +1046,7 @@ const emit = defineEmits(['a', 'b']) literalUnionNumber: BindingTypes.PROPS, literalUnionMixed: BindingTypes.PROPS, intersection: BindingTypes.PROPS, + intersection2: BindingTypes.PROPS, foo: BindingTypes.PROPS }) }) diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index 1b13c90b6ee..922351d9d45 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -2088,6 +2088,7 @@ function inferRuntimeType( case 'TSParenthesizedType': return inferRuntimeType(node.typeAnnotation, declaredTypes) case 'TSUnionType': + case 'TSIntersectionType': return [ ...new Set( [].concat( @@ -2095,8 +2096,6 @@ function inferRuntimeType( ) ) ] - case 'TSIntersectionType': - return ['Object'] case 'TSSymbolKeyword': return ['Symbol']