From 2587999cd508981893a0986e77d133312ed406c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=B8=89=E5=92=B2=E6=99=BA=E5=AD=90?= Date: Sun, 13 Nov 2022 05:19:46 +0800 Subject: [PATCH] fix(compiler-sfc): TSTypeLiteral could a function --- .../__snapshots__/compileScript.spec.ts.snap | 1 + .../compiler-sfc/__tests__/compileScript.spec.ts | 8 ++++++++ packages/compiler-sfc/src/compileScript.ts | 16 ++++++++++++++-- 3 files changed, 23 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 649b92d17f0..1aa0cbd68af 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -1603,6 +1603,7 @@ export default /*#__PURE__*/_defineComponent({ alias: { type: Array, required: true }, method: { type: Function, required: true }, symbol: { type: Symbol, required: true }, + objectOrFn: { type: [Function, Object], required: true }, union: { type: [String, Number], required: true }, literalUnion: { type: String, required: true }, literalUnionNumber: { type: Number, required: true }, diff --git a/packages/compiler-sfc/__tests__/compileScript.spec.ts b/packages/compiler-sfc/__tests__/compileScript.spec.ts index 3ea7632f68b..c632273c401 100644 --- a/packages/compiler-sfc/__tests__/compileScript.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript.spec.ts @@ -898,6 +898,10 @@ const emit = defineEmits(['a', 'b']) alias: Alias method(): void symbol: symbol + objectOrFn: { + (): void + foo: string + } union: string | number literalUnion: 'foo' | 'bar' @@ -928,6 +932,9 @@ const emit = defineEmits(['a', 'b']) expect(content).toMatch(`alias: { type: Array, required: true }`) expect(content).toMatch(`method: { type: Function, required: true }`) expect(content).toMatch(`symbol: { type: Symbol, required: true }`) + expect(content).toMatch( + `objectOrFn: { type: [Function, Object], required: true },` + ) expect(content).toMatch( `union: { type: [String, Number], required: true }` ) @@ -961,6 +968,7 @@ const emit = defineEmits(['a', 'b']) alias: BindingTypes.PROPS, method: BindingTypes.PROPS, symbol: BindingTypes.PROPS, + objectOrFn: BindingTypes.PROPS, union: BindingTypes.PROPS, literalUnion: BindingTypes.PROPS, literalUnionNumber: BindingTypes.PROPS, diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index b7e4c0ea778..15fa1949c99 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -1920,9 +1920,21 @@ function inferRuntimeType( return ['Boolean'] case 'TSObjectKeyword': return ['Object'] - case 'TSTypeLiteral': + case 'TSTypeLiteral': { // TODO (nice to have) generate runtime property validation - return ['Object'] + const types = new Set() + for (const m of node.members) { + switch (m.type) { + case 'TSCallSignatureDeclaration': + case 'TSConstructSignatureDeclaration': + types.add('Function') + break + default: + types.add('Object') + } + } + return Array.from(types) + } case 'TSFunctionType': return ['Function'] case 'TSArrayType':