]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(types): allow assigning wider SetupContext type (#2818)
author翠 / green <green@sapphi.red>
Mon, 14 Nov 2022 09:33:29 +0000 (18:33 +0900)
committerGitHub <noreply@github.com>
Mon, 14 Nov 2022 09:33:29 +0000 (04:33 -0500)
fix #2362

packages/runtime-core/src/component.ts
test-dts/component.test-d.ts

index 3fdf566bb98db76ba1405fea527d5be042f8510b..786e3f3a0302bac719e19d3078b7fa08b55161ac 100644 (file)
@@ -180,12 +180,15 @@ export const enum LifecycleHooks {
   SERVER_PREFETCH = 'sp'
 }
 
-export interface SetupContext<E = EmitsOptions> {
-  attrs: Data
-  slots: Slots
-  emit: EmitFn<E>
-  expose: (exposed?: Record<string, any>) => void
-}
+// use `E extends any` to force evaluating type to fix #2362
+export type SetupContext<E = EmitsOptions> = E extends any
+  ? {
+      attrs: Data
+      slots: Slots
+      emit: EmitFn<E>
+      expose: (exposed?: Record<string, any>) => void
+    }
+  : never
 
 /**
  * @internal
index 3463995613c6056c55a1887ecd646c8d477e3030..5678c8e1ceb66c98365ab82ae391918389cc8334 100644 (file)
@@ -11,7 +11,9 @@ import {
   FunctionalComponent,
   ComponentPublicInstance,
   toRefs,
-  IsAny
+  IsAny,
+  SetupContext,
+  expectAssignable
 } from './index'
 
 declare function extractComponentOptions<Props, RawBindings>(
@@ -476,3 +478,11 @@ describe('class', () => {
 
   expectType<number>(props.foo)
 })
+
+describe('SetupContext', () => {
+  describe('can assign', () => {
+    const wider: SetupContext<{ a: () => true; b: () => true }> = {} as any
+
+    expectAssignable<SetupContext<{ b: () => true }>>(wider)
+  })
+})