فهرست منبع

types(runtime-core): make `this` void in prop validators and prop default factories (#1867)

Aurelius333 5 سال پیش
والد
کامیت
a88895b5da
2فایلهای تغییر یافته به همراه22 افزوده شده و 1 حذف شده
  1. 1 1
      packages/runtime-core/src/componentOptions.ts
  2. 21 0
      test-dts/defineComponent.test-d.tsx

+ 1 - 1
packages/runtime-core/src/componentOptions.ts

@@ -217,7 +217,7 @@ export type ComponentOptionsWithObjectProps<
   EE extends string = string,
   Props = Readonly<ExtractPropTypes<PropsOptions>>
 > = ComponentOptionsBase<Props, RawBindings, D, C, M, Mixin, Extends, E, EE> & {
-  props: PropsOptions
+  props: PropsOptions & ThisType<void>
 } & ThisType<
     CreateComponentPublicInstance<
       Props,

+ 21 - 0
test-dts/defineComponent.test-d.tsx

@@ -210,6 +210,27 @@ describe('with object props', () => {
   )
   // @ts-expect-error
   expectError(<MyComponent b="foo" dd={{ n: 'string' }} ddd={['foo']} />)
+
+  // `this` should be void inside of prop validators and prop default factories
+  defineComponent({
+    props: {
+      myProp: {
+        type: Number,
+        validator(val: unknown): boolean {
+          // @ts-expect-error
+          return val !== this.otherProp
+        },
+        default(): number {
+          // @ts-expect-error
+          return this.otherProp + 1
+        }
+      },
+      otherProp: {
+        type: Number,
+        required: true
+      }
+    }
+  })
 })
 
 // describe('type inference w/ optional props declaration', () => {