diff --git a/packages/uni-app-uts/__tests__/android/sfc/compileScript/__snapshots__/defineProps.spec.ts.snap b/packages/uni-app-uts/__tests__/android/sfc/compileScript/__snapshots__/defineProps.spec.ts.snap index 560f0adadd34bb725a15fc633c19e0e99e690f33..1288fa073c13c10cfb9fb426bfc0b41e4bb311d1 100644 --- a/packages/uni-app-uts/__tests__/android/sfc/compileScript/__snapshots__/defineProps.spec.ts.snap +++ b/packages/uni-app-uts/__tests__/android/sfc/compileScript/__snapshots__/defineProps.spec.ts.snap @@ -328,7 +328,8 @@ export default { unknownUnion: { type: null, required: true }, unknownIntersection: { type: Object, required: true }, unknownUnionWithBoolean: { type: Boolean, required: true, skipCheck: true }, - unknownUnionWithFunction: { type: Function, required: true, skipCheck: true } + unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }, + propType: { type: Object as PropType<()=>string>, required: true, skipCheck: true } }, setup(__props: GenAnonymous) { const __ins = getCurrentInstance()!; diff --git a/packages/uni-app-uts/__tests__/android/sfc/compileScript/defineProps.spec.ts b/packages/uni-app-uts/__tests__/android/sfc/compileScript/defineProps.spec.ts index 6e5cbc37c4527768835d48681c826579236f20b7..a0d054a505a3ecc20ad3bd2071645c22dbfc7d91 100644 --- a/packages/uni-app-uts/__tests__/android/sfc/compileScript/defineProps.spec.ts +++ b/packages/uni-app-uts/__tests__/android/sfc/compileScript/defineProps.spec.ts @@ -130,6 +130,9 @@ const props = defineProps({ foo: String }) unknownIntersection: UnknownType & Object unknownUnionWithBoolean: UnknownType | boolean unknownUnionWithFunction: UnknownType | (() => any) + + propType: PropType<()=>string> + }>() `) assertCode(content) @@ -188,6 +191,9 @@ const props = defineProps({ foo: String }) expect(content).toMatch( `unknownUnionWithFunction: { type: Function, required: true, skipCheck: true }` ) + expect(content).toMatch( + `propType: { type: Object as PropType<()=>string>, required: true, skipCheck: true }` + ) expect(bindings).toStrictEqual({ string: BindingTypes.PROPS, number: BindingTypes.PROPS, @@ -228,6 +234,7 @@ const props = defineProps({ foo: String }) unknownIntersection: BindingTypes.PROPS, unknownUnionWithBoolean: BindingTypes.PROPS, unknownUnionWithFunction: BindingTypes.PROPS, + propType: BindingTypes.PROPS, }) }) diff --git a/packages/uni-app-uts/src/plugins/android/uvue/sfc/compiler/script/defineProps.ts b/packages/uni-app-uts/src/plugins/android/uvue/sfc/compiler/script/defineProps.ts index ccee55926475c9bd5a0149b23d0fece55e1b00e0..9e5683c5167321282172f3e2d5355caea9a0afa3 100644 --- a/packages/uni-app-uts/src/plugins/android/uvue/sfc/compiler/script/defineProps.ts +++ b/packages/uni-app-uts/src/plugins/android/uvue/sfc/compiler/script/defineProps.ts @@ -5,6 +5,8 @@ import { ObjectMethod, ObjectExpression, Expression, + TSPropertySignature, + TSMethodSignature, } from '@babel/types' import { BindingTypes, isFunctionType } from '@vue/compiler-dom' import { ScriptCompileContext } from './context' @@ -225,6 +227,32 @@ function genRuntimePropsFromTypes(ctx: ScriptCompileContext) { return propsDecls } +function parsePropType( + ctx: ScriptCompileContext, + node: TSMethodSignature | TSPropertySignature +) { + if (node.type === 'TSPropertySignature') { + const typeAnn = node.typeAnnotation + if (typeAnn) { + const tsType = typeAnn?.typeAnnotation + if (tsType?.type === 'TSTypeReference') { + if ( + tsType.typeName.type === 'Identifier' && + tsType.typeName.name === 'PropType' + ) { + return [ + `Object as ${ctx.source.slice( + tsType.start! + ctx.startOffset!, + tsType.end! + ctx.startOffset! + )}`, + ] + } + } + } + } + return [] +} + function resolveRuntimePropsFromType( ctx: ScriptCompileContext, node: Node @@ -233,15 +261,20 @@ function resolveRuntimePropsFromType( const elements = resolveTypeElements(ctx, node) for (const key in elements.props) { const e = elements.props[key] - let type = inferRuntimeType(ctx, e) + let type = parsePropType(ctx, e) let skipCheck = false - // skip check for result containing unknown types - if (type.includes(UNKNOWN_TYPE)) { - if (type.includes('Boolean') || type.includes('Function')) { - type = type.filter((t) => t !== UNKNOWN_TYPE) - skipCheck = true - } else { - type = ['null'] + if (type.length) { + skipCheck = true + } else { + type = inferRuntimeType(ctx, e) + // skip check for result containing unknown types + if (type.includes(UNKNOWN_TYPE)) { + if (type.includes('Boolean') || type.includes('Function')) { + type = type.filter((t) => t !== UNKNOWN_TYPE) + skipCheck = true + } else { + type = ['null'] + } } } props.push({