Skip to content

Commit eb2a832

Browse files
authoredNov 14, 2022
fix(types): allow assigning wider SetupContext type (#2818)
fix #2362
1 parent 588bd44 commit eb2a832

File tree

2 files changed

+20
-7
lines changed

2 files changed

+20
-7
lines changed
 

‎packages/runtime-core/src/component.ts

+9-6
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,15 @@ export const enum LifecycleHooks {
180180
SERVER_PREFETCH = 'sp'
181181
}
182182

183-
export interface SetupContext<E = EmitsOptions> {
184-
attrs: Data
185-
slots: Slots
186-
emit: EmitFn<E>
187-
expose: (exposed?: Record<string, any>) => void
188-
}
183+
// use `E extends any` to force evaluating type to fix #2362
184+
export type SetupContext<E = EmitsOptions> = E extends any
185+
? {
186+
attrs: Data
187+
slots: Slots
188+
emit: EmitFn<E>
189+
expose: (exposed?: Record<string, any>) => void
190+
}
191+
: never
189192

190193
/**
191194
* @internal

‎test-dts/component.test-d.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ import {
1111
FunctionalComponent,
1212
ComponentPublicInstance,
1313
toRefs,
14-
IsAny
14+
IsAny,
15+
SetupContext,
16+
expectAssignable
1517
} from './index'
1618

1719
declare function extractComponentOptions<Props, RawBindings>(
@@ -476,3 +478,11 @@ describe('class', () => {
476478

477479
expectType<number>(props.foo)
478480
})
481+
482+
describe('SetupContext', () => {
483+
describe('can assign', () => {
484+
const wider: SetupContext<{ a: () => true; b: () => true }> = {} as any
485+
486+
expectAssignable<SetupContext<{ b: () => true }>>(wider)
487+
})
488+
})

0 commit comments

Comments
 (0)
Please sign in to comment.