From 69995a44437488babbe173c8b2f055bca35545fb Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 22 Apr 2024 15:36:38 +0800 Subject: [PATCH] fix(compiler-sfc): handle readonly operator and ReadonlyArray/Map/Set types close #10726 --- .../compileScript/resolveType.spec.ts | 31 +++++++++++++++++++ .../compiler-sfc/src/script/resolveType.ts | 11 +++++++ 2 files changed, 42 insertions(+) diff --git a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts index f3be58a301c..dc95a9dc643 100644 --- a/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts +++ b/packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts @@ -265,6 +265,27 @@ describe('resolveType', () => { }) }) + test('utility type: ReadonlyArray', () => { + expect( + resolve(` + defineProps<{ foo: ReadonlyArray }>() + `).props, + ).toStrictEqual({ + foo: ['Array'], + }) + }) + + test('utility type: ReadonlyMap & Readonly Set', () => { + expect( + resolve(` + defineProps<{ foo: ReadonlyMap, bar: ReadonlySet }>() + `).props, + ).toStrictEqual({ + foo: ['Map'], + bar: ['Set'], + }) + }) + test('indexed access type (literal)', () => { expect( resolve(` @@ -416,6 +437,16 @@ describe('resolveType', () => { }) }) + test('readonly', () => { + expect( + resolve(` + defineProps<{ foo: readonly unknown[] }>() + `).props, + ).toStrictEqual({ + foo: ['Array'], + }) + }) + test('ExtractPropTypes (element-plus)', () => { const { props, raw } = resolve( ` diff --git a/packages/compiler-sfc/src/script/resolveType.ts b/packages/compiler-sfc/src/script/resolveType.ts index f6e291791a8..54b207e7e91 100644 --- a/packages/compiler-sfc/src/script/resolveType.ts +++ b/packages/compiler-sfc/src/script/resolveType.ts @@ -1547,8 +1547,14 @@ export function inferRuntimeType( case 'Parameters': case 'ConstructorParameters': + case 'ReadonlyArray': return ['Array'] + case 'ReadonlyMap': + return ['Map'] + case 'ReadonlySet': + return ['Set'] + case 'NonNullable': if (node.typeParameters && node.typeParameters.params[0]) { return inferRuntimeType( @@ -1633,6 +1639,11 @@ export function inferRuntimeType( } break } + + // e.g. readonly + case 'TSTypeOperator': { + return inferRuntimeType(ctx, node.typeAnnotation, scope) + } } } catch (e) { // always soft fail on failed runtime type inference