]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
feat(compiler-sfc): improve utility type Partial and Required (#8103)
authoredison <daiwei521@126.com>
Thu, 20 Apr 2023 13:10:38 +0000 (21:10 +0800)
committerEvan You <yyx990803@gmail.com>
Thu, 20 Apr 2023 13:17:10 +0000 (21:17 +0800)
packages/compiler-sfc/__tests__/compileScript/resolveType.spec.ts
packages/compiler-sfc/src/script/resolveType.ts

index dc859f3338cc92fc14118d86f9fd950fd2d3c4b3..c7e2b9b694a984b1491a7d99f5739f417bf5117d 100644 (file)
@@ -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(`
index 79ee256703571a2eb3f12f95b280cde0cf072027..526569843ecad706257c55c63be9a85b475a6c4a 100644 (file)
@@ -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': {