]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(runtime-core): argument for props validator
authorEvan You <yyx990803@gmail.com>
Tue, 18 Aug 2020 15:37:34 +0000 (11:37 -0400)
committerEvan You <yyx990803@gmail.com>
Tue, 18 Aug 2020 15:37:34 +0000 (11:37 -0400)
Unfortunately it requires explicit annotation for now

packages/runtime-core/src/componentProps.ts
test-dts/defineComponent.test-d.tsx

index 9e6540cfa1c56408b254a8d9bf868bde116a77a3..b07ca81d875026cd090582c4c170abea3bfe9f2f 100644 (file)
@@ -42,7 +42,7 @@ export type ComponentObjectPropsOptions<P = Data> = {
 
 export type Prop<T, D = T> = PropOptions<T, D> | PropType<T>
 
-type DefaultFactory<T> = () => T | null | undefined
+type DefaultFactory<T> = (props: Data) => T | null | undefined
 
 interface PropOptions<T = any, D = T> {
   type?: PropType<T> | true | null
index 41b26f266ab28f1ee101fa3bac83d247e7955802..6918db61c8e695c946217ea9d90f1af424ef5f47 100644 (file)
@@ -18,6 +18,7 @@ describe('with object props', () => {
     b: string
     e?: Function
     bb: string
+    bbb: string
     cc?: string[] | undefined
     dd: { n: 1 }
     ee?: () => string
@@ -46,6 +47,11 @@ describe('with object props', () => {
       bb: {
         default: 'hello'
       },
+      bbb: {
+        // Note: default function value requires arrow syntax + explicit
+        // annotation
+        default: (props: any) => (props.bb as string) || 'foo'
+      },
       // explicit type casting
       cc: Array as PropType<string[]>,
       // required + type casting
@@ -85,6 +91,7 @@ describe('with object props', () => {
       },
       validated: {
         type: String,
+        // validator requires explicit annotation
         validator: (val: unknown) => val !== ''
       }
     },
@@ -94,6 +101,7 @@ describe('with object props', () => {
       expectType<ExpectedProps['b']>(props.b)
       expectType<ExpectedProps['e']>(props.e)
       expectType<ExpectedProps['bb']>(props.bb)
+      expectType<ExpectedProps['bbb']>(props.bbb)
       expectType<ExpectedProps['cc']>(props.cc)
       expectType<ExpectedProps['dd']>(props.dd)
       expectType<ExpectedProps['ee']>(props.ee)