]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): respect props with default on instance type when using __typeProps
authorEvan You <yyx990803@gmail.com>
Wed, 5 Jun 2024 06:19:31 +0000 (14:19 +0800)
committerEvan You <yyx990803@gmail.com>
Wed, 5 Jun 2024 06:19:31 +0000 (14:19 +0800)
packages/dts-test/defineComponent.test-d.tsx
packages/runtime-core/src/componentPublicInstance.ts

index 0d9aa71dc5e7a3daec32ddb13708667b81e3e676..79c2e677d2df5583ca9f550e6c1254080e188d84 100644 (file)
@@ -1972,3 +1972,24 @@ createApp({}).component(
     },
   }),
 )
+
+const Comp = defineComponent({
+  props: {
+    actionText: {
+      type: {} as PropType<string>,
+      default: 'Become a sponsor',
+    },
+  },
+  __typeProps: {} as {
+    actionText?: string
+  },
+})
+
+const instance = new Comp()
+function expectString(s: string) {}
+// instance prop with default should be non-null
+expectString(instance.actionText)
+
+// public prop on $props should be optional
+// @ts-expect-error
+expectString(instance.$props.actionText)
index 8f244ac1d94dc9984ba2e69e2ffb53e4cdc3b5df..d7cfc5bea25b725fb79fabb581463ce0e2ef426f 100644 (file)
@@ -292,7 +292,7 @@ export type ComponentPublicInstance<
   C extends ComputedOptions = {},
   M extends MethodOptions = {},
   E extends EmitsOptions = {},
-  PublicProps = P,
+  PublicProps = {},
   Defaults = {},
   MakeDefaultsOptional extends boolean = false,
   Options = ComponentOptionsBase<any, any, any, any, any, any, any, any, any>,
@@ -323,7 +323,11 @@ export type ComponentPublicInstance<
     options?: WatchOptions,
   ): WatchStopHandle
 } & ExposedKeys<
-  IfAny<P, P, Omit<P, keyof ShallowUnwrapRef<B>>> &
+  IfAny<
+    P,
+    P,
+    Readonly<Defaults> & Omit<P, keyof ShallowUnwrapRef<B> | keyof Defaults>
+  > &
     ShallowUnwrapRef<B> &
     UnwrapNestedRefs<D> &
     ExtractComputedReturns<C> &