Skip to content

Commit

Permalink
fix(compiler-sfc): handle readonly operator and ReadonlyArray/Map/Set…
Browse files Browse the repository at this point in the history
… types

close #10726
  • Loading branch information
yyx990803 committed Apr 22, 2024
1 parent 65109a7 commit 5cef52a
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
31 changes: 31 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,27 @@ describe('resolveType', () => {
})
})

test('utility type: ReadonlyArray', () => {
expect(
resolve(`
defineProps<{ foo: ReadonlyArray<string> }>()
`).props,
).toStrictEqual({
foo: ['Array'],
})
})

test('utility type: ReadonlyMap & Readonly Set', () => {
expect(
resolve(`
defineProps<{ foo: ReadonlyMap<string, unknown>, bar: ReadonlySet<string> }>()
`).props,
).toStrictEqual({
foo: ['Map'],
bar: ['Set'],
})
})

test('indexed access type (literal)', () => {
expect(
resolve(`
Expand Down Expand Up @@ -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(
`
Expand Down
11 changes: 11 additions & 0 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5cef52a

Please sign in to comment.