Skip to content

Commit

Permalink
fix(runtime-core): force evalutating SetupContext type (fix vuejs#2362)
Browse files Browse the repository at this point in the history
  • Loading branch information
sapphi-red committed Dec 14, 2020
1 parent 3ca7336 commit 1383f6e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 9 additions & 6 deletions packages/runtime-core/src/component.ts
Expand Up @@ -167,12 +167,15 @@ export const enum LifecycleHooks {
ERROR_CAPTURED = 'ec'
}

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
Expand Down
12 changes: 11 additions & 1 deletion test-dts/component.test-d.ts
Expand Up @@ -9,7 +9,9 @@ import {
expectType,
ShallowUnwrapRef,
FunctionalComponent,
ComponentPublicInstance
ComponentPublicInstance,
SetupContext,
expectAssignable
} from './index'

declare function extractComponentOptions<Props, RawBindings>(
Expand Down Expand Up @@ -423,3 +425,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)
})
})

0 comments on commit 1383f6e

Please sign in to comment.