Browse Source

fix(types): return type of withDefaults should be readonly (#8601)

zqran 2 năm trước cách đây
mục cha
commit
f15debc01a

+ 5 - 0
packages/dts-test/setupHelpers.test-d.ts

@@ -110,6 +110,7 @@ describe('defineProps w/ generic type declaration + withDefaults', <T extends
     defineProps<{
       n?: number
       bool?: boolean
+      s?: string
 
       generic1?: T[] | { x: T }
       generic2?: { x: T }
@@ -128,6 +129,10 @@ describe('defineProps w/ generic type declaration + withDefaults', <T extends
   )
 
   res.n + 1
+  // @ts-expect-error should be readonly
+  res.n++
+  // @ts-expect-error should be readonly
+  res.s = ''
 
   expectType<T[] | { x: T }>(res.generic1)
   expectType<{ x: T }>(res.generic2)

+ 2 - 2
packages/runtime-core/src/apiSetupHelpers.ts

@@ -298,8 +298,8 @@ type PropsWithDefaults<
   T,
   Defaults extends InferDefaults<T>,
   BKeys extends keyof T
-> = Omit<T, keyof Defaults> & {
-  [K in keyof Defaults]-?: K extends keyof T
+> = Readonly<Omit<T, keyof Defaults>> & {
+  readonly [K in keyof Defaults]-?: K extends keyof T
     ? Defaults[K] extends undefined
       ? T[K]
       : NotUndefined<T[K]>