Skip to content

Commit 1d1d728

Browse files
edison1105yyx990803
authored andcommitted
feat(compiler-sfc): improve utility type Partial and Required (#8103)
1 parent 5cdaac2 commit 1d1d728

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,38 @@ describe('resolveType', () => {
207207
})
208208
})
209209

210+
test('utility type: Partial', () => {
211+
expect(
212+
resolve(`
213+
type T = { foo: number, bar: string }
214+
defineProps<Partial<T>>()
215+
`).raw.props
216+
).toMatchObject({
217+
foo: {
218+
optional: true
219+
},
220+
bar: {
221+
optional: true
222+
}
223+
})
224+
})
225+
226+
test('utility type: Required', () => {
227+
expect(
228+
resolve(`
229+
type T = { foo?: number, bar?: string }
230+
defineProps<Required<T>>()
231+
`).raw.props
232+
).toMatchObject({
233+
foo: {
234+
optional: false
235+
},
236+
bar: {
237+
optional: false
238+
}
239+
})
240+
})
241+
210242
test('utility type: Pick', () => {
211243
expect(
212244
resolve(`

packages/compiler-sfc/src/script/resolveType.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,20 @@ function resolveBuiltin(
513513
): ResolvedElements {
514514
const t = resolveTypeElements(ctx, node.typeParameters!.params[0])
515515
switch (name) {
516-
case 'Partial':
517-
case 'Required':
516+
case 'Partial': {
517+
const res: ResolvedElements = { props: {}, calls: t.calls }
518+
Object.keys(t.props).forEach(key => {
519+
res.props[key] = { ...t.props[key], optional: true }
520+
})
521+
return res
522+
}
523+
case 'Required': {
524+
const res: ResolvedElements = { props: {}, calls: t.calls }
525+
Object.keys(t.props).forEach(key => {
526+
res.props[key] = { ...t.props[key], optional: false }
527+
})
528+
return res
529+
}
518530
case 'Readonly':
519531
return t
520532
case 'Pick': {

0 commit comments

Comments
 (0)