Просмотр исходного кода

fix(types): allow assigning wider SetupContext type (#2818)

fix #2362
翠 / green 3 лет назад
Родитель
Сommit
eb2a83283c
2 измененных файлов с 20 добавлено и 7 удалено
  1. 9 6
      packages/runtime-core/src/component.ts
  2. 11 1
      test-dts/component.test-d.ts

+ 9 - 6
packages/runtime-core/src/component.ts

@@ -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

+ 11 - 1
test-dts/component.test-d.ts

@@ -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)
+  })
+})