]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
types(runtime-core): improve the extracted instance types (#1936)
authorHcySunYang <HcySunYang@outlook.com>
Tue, 25 Aug 2020 01:37:22 +0000 (09:37 +0800)
committerGitHub <noreply@github.com>
Tue, 25 Aug 2020 01:37:22 +0000 (21:37 -0400)
packages/runtime-core/src/apiDefineComponent.ts
test-dts/defineComponent.test-d.tsx

index 4a100d8f6c58eb3dd4ef4ec9c15fedc04c3cc93e..eda62eaab03adc299822f6f6d43289806e29eeaf 100644 (file)
@@ -189,7 +189,8 @@ export function defineComponent<
     Extends,
     E,
     VNodeProps & AllowedComponentProps & ComponentCustomProps
-  >
+  > &
+    Readonly<ExtractPropTypes<PropsOptions>>
 > &
   ComponentOptionsWithObjectProps<
     PropsOptions,
index 7a88c40bc4e51d329354d61a1987456075531052..cd08a428893cb9ebd2d83bad2c83951ee84fae6f 100644 (file)
@@ -94,7 +94,7 @@ describe('with object props', () => {
       // default + function
       ffff: {
         type: Function as PropType<(a: number, b: string) => { a: boolean }>,
-        default: (a: number, b: string) => ({ a: true })
+        default: (a: number, b: string) => ({ a: a > +b })
       },
       validated: {
         type: String,
@@ -799,3 +799,58 @@ describe('componentOptions setup should be `SetupContext`', () => {
     ctx: SetupContext
   ) => any)
 })
+
+describe('extract instance type', () => {
+  const Base = defineComponent({
+    props: {
+      baseA: {
+        type: Number,
+        default: 1
+      }
+    }
+  })
+  const MixinA = defineComponent({
+    props: {
+      mA: {
+        type: String,
+        default: ''
+      }
+    }
+  })
+  const CompA = defineComponent({
+    extends: Base,
+    mixins: [MixinA],
+    props: {
+      a: {
+        type: Boolean,
+        default: false
+      },
+      b: {
+        type: String,
+        required: true
+      },
+      c: Number
+    }
+  })
+
+  const compA = {} as InstanceType<typeof CompA>
+
+  expectType<boolean>(compA.a)
+  expectType<string>(compA.b)
+  expectType<number | undefined>(compA.c)
+  // mixins
+  expectType<string>(compA.mA)
+  // extends
+  expectType<number>(compA.baseA)
+
+  //  @ts-expect-error
+  expectError((compA.a = true))
+  //  @ts-expect-error
+  expectError((compA.b = 'foo'))
+  //  @ts-expect-error
+  expectError((compA.c = 1))
+  //  @ts-expect-error
+  expectError((compA.mA = 'foo'))
+  //  @ts-expect-error
+  expectError((compA.baseA = 1))
+})