Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(types): fix defineComponent inference to Component (#5949)
  • Loading branch information
pikax committed May 18, 2022
1 parent 3e2850f commit 7c8f457
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 5 deletions.
72 changes: 69 additions & 3 deletions packages/runtime-core/src/apiDefineComponent.ts
Expand Up @@ -111,7 +111,29 @@ export function defineComponent<
E extends EmitsOptions = EmitsOptions,
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Options extends {} = {}
Options extends ComponentOptionsWithoutProps<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
> = ComponentOptionsWithoutProps<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
>
>(
options: Options &
ComponentOptionsWithoutProps<
Expand Down Expand Up @@ -154,7 +176,29 @@ export function defineComponent<
E extends EmitsOptions = Record<string, any>,
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Options extends {} = {}
Options extends ComponentOptionsWithArrayProps<
PropNames,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
> = ComponentOptionsWithArrayProps<
PropNames,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
>
>(
options: Options &
ComponentOptionsWithArrayProps<
Expand Down Expand Up @@ -198,7 +242,29 @@ export function defineComponent<
E extends EmitsOptions = Record<string, any>,
EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Options extends {} = {}
Options extends ComponentOptionsWithObjectProps<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
> = ComponentOptionsWithObjectProps<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide
>
>(
options: Options &
ComponentOptionsWithObjectProps<
Expand Down
17 changes: 16 additions & 1 deletion test-dts/defineComponent.test-d.tsx
@@ -1,5 +1,6 @@
import {
describe,
test,
Component,
defineComponent,
PropType,
Expand Down Expand Up @@ -1045,7 +1046,7 @@ describe('emits', () => {
})

describe('componentOptions setup should be `SetupContext`', () => {
expect<ComponentOptions['setup']>(
expectType<ComponentOptions['setup']>(
{} as (props: Record<string, any>, ctx: SetupContext) => any
)
})
Expand Down Expand Up @@ -1141,6 +1142,20 @@ describe('async setup', () => {
vm.a = 2
})

// #5948
describe('DefineComponent should infer correct types when assigning to Component', () => {
let component: Component
component = defineComponent({
setup(_, { attrs, slots }) {
// @ts-expect-error should not be any
expectType<[]>(attrs)
// @ts-expect-error should not be any
expectType<[]>(slots)
}
})
expectType<Component>(component)
})

// check if defineComponent can be exported
export default {
// function components
Expand Down
3 changes: 2 additions & 1 deletion test-dts/index.d.ts
Expand Up @@ -4,6 +4,7 @@
export * from '@vue/runtime-dom'

export function describe(_name: string, _fn: () => void): void
export function test(_name: string, _fn: () => any): void

export function expectType<T>(value: T): void
export function expectError<T>(value: T): void
Expand All @@ -15,4 +16,4 @@ export type IsUnion<T, U extends T = T> = (
? false
: true

export type IsAny<T> = 0 extends (1 & T) ? true : false
export type IsAny<T> = 0 extends 1 & T ? true : false

0 comments on commit 7c8f457

Please sign in to comment.