Skip to content

Commit

Permalink
feat(compiler-sfc): improve utility type Partial and Required (#8103)
Browse files Browse the repository at this point in the history
  • Loading branch information
edison1105 authored and yyx990803 committed Apr 20, 2023
1 parent 5cdaac2 commit 1d1d728
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 2 deletions.
32 changes: 32 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,38 @@ describe('resolveType', () => {
})
})

test('utility type: Partial', () => {
expect(
resolve(`
type T = { foo: number, bar: string }
defineProps<Partial<T>>()
`).raw.props
).toMatchObject({
foo: {
optional: true
},
bar: {
optional: true
}
})
})

test('utility type: Required', () => {
expect(
resolve(`
type T = { foo?: number, bar?: string }
defineProps<Required<T>>()
`).raw.props
).toMatchObject({
foo: {
optional: false
},
bar: {
optional: false
}
})
})

test('utility type: Pick', () => {
expect(
resolve(`
Expand Down
16 changes: 14 additions & 2 deletions packages/compiler-sfc/src/script/resolveType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,8 +513,20 @@ function resolveBuiltin(
): ResolvedElements {
const t = resolveTypeElements(ctx, node.typeParameters!.params[0])
switch (name) {
case 'Partial':
case 'Required':
case 'Partial': {
const res: ResolvedElements = { props: {}, calls: t.calls }
Object.keys(t.props).forEach(key => {
res.props[key] = { ...t.props[key], optional: true }
})
return res
}
case 'Required': {
const res: ResolvedElements = { props: {}, calls: t.calls }
Object.keys(t.props).forEach(key => {
res.props[key] = { ...t.props[key], optional: false }
})
return res
}
case 'Readonly':
return t
case 'Pick': {
Expand Down

0 comments on commit 1d1d728

Please sign in to comment.