fix(types): restore DefineComponent argument order

This is necessary for compatibility w/ tsc-generated component types
using DefineComponent.

revert #5416
reopen #3796
fix #5967
This commit is contained in:
Evan You 2022-05-21 00:53:46 +08:00
parent 583b625987
commit 8071ef47b5
5 changed files with 88 additions and 210 deletions

View File

@ -111,7 +111,7 @@ export function defineAsyncComponent<
) )
} }
return defineComponent<{}>({ return defineComponent({
name: 'AsyncComponentWrapper', name: 'AsyncComponentWrapper',
__asyncLoader: load, __asyncLoader: load,

View File

@ -6,8 +6,7 @@ import {
ComponentOptionsWithObjectProps, ComponentOptionsWithObjectProps,
ComponentOptionsMixin, ComponentOptionsMixin,
RenderFunction, RenderFunction,
ComponentOptionsBase, ComponentOptionsBase
ComponentProvideOptions
} from './componentOptions' } from './componentOptions'
import { import {
SetupContext, SetupContext,
@ -41,8 +40,6 @@ export type DefineComponent<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = {}, E extends EmitsOptions = {},
EE extends string = string, EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
RawOptions extends {} = {},
PP = PublicProps, PP = PublicProps,
Props = Readonly< Props = Readonly<
PropsOrPropOptions extends ComponentPropsOptions PropsOrPropOptions extends ComponentPropsOptions
@ -51,23 +48,22 @@ export type DefineComponent<
> & > &
({} extends E ? {} : EmitsToProps<E>), ({} extends E ? {} : EmitsToProps<E>),
Defaults = ExtractDefaultPropTypes<PropsOrPropOptions> Defaults = ExtractDefaultPropTypes<PropsOrPropOptions>
> = RawOptions & > = ComponentPublicInstanceConstructor<
ComponentPublicInstanceConstructor< CreateComponentPublicInstance<
CreateComponentPublicInstance< Props,
Props, RawBindings,
RawBindings, D,
D, C,
C, M,
M, Mixin,
Mixin, Extends,
Extends, E,
E, PP & Props,
PP & Props, Defaults,
Defaults, true
true
> &
Props
> & > &
Props
> &
ComponentOptionsBase< ComponentOptionsBase<
Props, Props,
RawBindings, RawBindings,
@ -78,8 +74,7 @@ export type DefineComponent<
Extends, Extends,
E, E,
EE, EE,
Defaults, Defaults
Provide
> & > &
PP PP
@ -109,58 +104,20 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions, E extends EmitsOptions = EmitsOptions,
EE extends string = string, EE extends string = string
Provide extends ComponentProvideOptions = ComponentProvideOptions,
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 & options: ComponentOptionsWithoutProps<
ComponentOptionsWithoutProps< Props,
Props, RawBindings,
RawBindings, D,
D, C,
C, M,
M, Mixin,
Mixin, Extends,
Extends, E,
E, EE
EE, >
Provide ): DefineComponent<Props, RawBindings, D, C, M, Mixin, Extends, E, EE>
>
): DefineComponent<
Props,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide,
Options
>
// overload 3: object format with array props declaration // overload 3: object format with array props declaration
// props inferred as { [key in PropNames]?: any } // props inferred as { [key in PropNames]?: any }
@ -174,45 +131,19 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>, E extends EmitsOptions = Record<string, any>,
EE extends string = string, EE extends string = string
Provide extends ComponentProvideOptions = ComponentProvideOptions,
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 & options: ComponentOptionsWithArrayProps<
ComponentOptionsWithArrayProps< PropNames,
PropNames, RawBindings,
RawBindings, D,
D, C,
C, M,
M, Mixin,
Mixin, Extends,
Extends, E,
E, EE
EE, >
Provide
>
): DefineComponent< ): DefineComponent<
Readonly<{ [key in PropNames]?: any }>, Readonly<{ [key in PropNames]?: any }>,
RawBindings, RawBindings,
@ -222,9 +153,7 @@ export function defineComponent<
Mixin, Mixin,
Extends, Extends,
E, E,
EE, EE
Provide,
Options
> >
// overload 4: object format with object props declaration // overload 4: object format with object props declaration
@ -240,58 +169,20 @@ export function defineComponent<
Mixin extends ComponentOptionsMixin = ComponentOptionsMixin, Mixin extends ComponentOptionsMixin = ComponentOptionsMixin,
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = Record<string, any>, E extends EmitsOptions = Record<string, any>,
EE extends string = string, EE extends string = string
Provide extends ComponentProvideOptions = ComponentProvideOptions,
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 & options: ComponentOptionsWithObjectProps<
ComponentOptionsWithObjectProps< PropsOptions,
PropsOptions, RawBindings,
RawBindings, D,
D, C,
C, M,
M, Mixin,
Mixin, Extends,
Extends, E,
E, EE
EE, >
Provide ): DefineComponent<PropsOptions, RawBindings, D, C, M, Mixin, Extends, E, EE>
>
): DefineComponent<
PropsOptions,
RawBindings,
D,
C,
M,
Mixin,
Extends,
E,
EE,
Provide,
Options
>
// implementation, close to no-op // implementation, close to no-op
export function defineComponent(options: unknown) { export function defineComponent(options: unknown) {

View File

@ -118,9 +118,8 @@ export interface ComponentOptionsBase<
Extends extends ComponentOptionsMixin, Extends extends ComponentOptionsMixin,
E extends EmitsOptions, E extends EmitsOptions,
EE extends string = string, EE extends string = string,
Defaults = {}, Defaults = {}
Provide extends ComponentProvideOptions = ComponentProvideOptions > extends LegacyOptions<Props, D, C, M, Mixin, Extends>,
> extends LegacyOptions<Props, D, C, M, Mixin, Extends, Provide>,
ComponentInternalOptions, ComponentInternalOptions,
ComponentCustomOptions { ComponentCustomOptions {
setup?: ( setup?: (
@ -226,7 +225,6 @@ export type ComponentOptionsWithoutProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions, E extends EmitsOptions = EmitsOptions,
EE extends string = string, EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
PE = Props & EmitsToProps<E> PE = Props & EmitsToProps<E>
> = ComponentOptionsBase< > = ComponentOptionsBase<
PE, PE,
@ -238,8 +236,7 @@ export type ComponentOptionsWithoutProps<
Extends, Extends,
E, E,
EE, EE,
{}, {}
Provide
> & { > & {
props?: undefined props?: undefined
} & ThisType< } & ThisType<
@ -256,7 +253,6 @@ export type ComponentOptionsWithArrayProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions, E extends EmitsOptions = EmitsOptions,
EE extends string = string, EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E> Props = Readonly<{ [key in PropNames]?: any }> & EmitsToProps<E>
> = ComponentOptionsBase< > = ComponentOptionsBase<
Props, Props,
@ -268,8 +264,7 @@ export type ComponentOptionsWithArrayProps<
Extends, Extends,
E, E,
EE, EE,
{}, {}
Provide
> & { > & {
props: PropNames[] props: PropNames[]
} & ThisType< } & ThisType<
@ -295,7 +290,6 @@ export type ComponentOptionsWithObjectProps<
Extends extends ComponentOptionsMixin = ComponentOptionsMixin, Extends extends ComponentOptionsMixin = ComponentOptionsMixin,
E extends EmitsOptions = EmitsOptions, E extends EmitsOptions = EmitsOptions,
EE extends string = string, EE extends string = string,
Provide extends ComponentProvideOptions = ComponentProvideOptions,
Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>, Props = Readonly<ExtractPropTypes<PropsOptions>> & EmitsToProps<E>,
Defaults = ExtractDefaultPropTypes<PropsOptions> Defaults = ExtractDefaultPropTypes<PropsOptions>
> = ComponentOptionsBase< > = ComponentOptionsBase<
@ -308,8 +302,7 @@ export type ComponentOptionsWithObjectProps<
Extends, Extends,
E, E,
EE, EE,
Defaults, Defaults
Provide
> & { > & {
props: PropsOptions & ThisType<void> props: PropsOptions & ThisType<void>
} & ThisType< } & ThisType<
@ -409,8 +402,7 @@ interface LegacyOptions<
C extends ComputedOptions, C extends ComputedOptions,
M extends MethodOptions, M extends MethodOptions,
Mixin extends ComponentOptionsMixin, Mixin extends ComponentOptionsMixin,
Extends extends ComponentOptionsMixin, Extends extends ComponentOptionsMixin
Provide extends ComponentProvideOptions = ComponentProvideOptions
> { > {
compatConfig?: CompatConfig compatConfig?: CompatConfig
@ -444,7 +436,7 @@ interface LegacyOptions<
computed?: C computed?: C
methods?: M methods?: M
watch?: ComponentWatchOptions watch?: ComponentWatchOptions
provide?: Provide provide?: ComponentProvideOptions
inject?: ComponentInjectOptions inject?: ComponentInjectOptions
// assets // assets

View File

@ -34,8 +34,7 @@ import {
OptionTypesKeys, OptionTypesKeys,
resolveMergedOptions, resolveMergedOptions,
shouldCacheAccess, shouldCacheAccess,
MergedComponentOptionsOverride, MergedComponentOptionsOverride
ComponentProvideOptions
} from './componentOptions' } from './componentOptions'
import { EmitsOptions, EmitFn } from './componentEmits' import { EmitsOptions, EmitFn } from './componentEmits'
import { Slots } from './componentSlots' import { Slots } from './componentSlots'
@ -151,8 +150,7 @@ export type CreateComponentPublicInstance<
PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> & PublicM extends MethodOptions = UnwrapMixinsType<PublicMixin, 'M'> &
EnsureNonVoid<M>, EnsureNonVoid<M>,
PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> & PublicDefaults = UnwrapMixinsType<PublicMixin, 'Defaults'> &
EnsureNonVoid<Defaults>, EnsureNonVoid<Defaults>
Provide extends ComponentProvideOptions = ComponentProvideOptions
> = ComponentPublicInstance< > = ComponentPublicInstance<
PublicP, PublicP,
PublicB, PublicB,
@ -163,19 +161,7 @@ export type CreateComponentPublicInstance<
PublicProps, PublicProps,
PublicDefaults, PublicDefaults,
MakeDefaultsOptional, MakeDefaultsOptional,
ComponentOptionsBase< ComponentOptionsBase<P, B, D, C, M, Mixin, Extends, E, string, Defaults>
P,
B,
D,
C,
M,
Mixin,
Extends,
E,
string,
Defaults,
Provide
>
> >
// public properties exposed on the proxy, which is used as the render context // public properties exposed on the proxy, which is used as the render context

View File

@ -264,18 +264,6 @@ describe('with object props', () => {
expectType<Component>(MyComponent) expectType<Component>(MyComponent)
expectType<typeof props>(MyComponent.props)
// @ts-expect-error it should be an object and not any
expectError<[]>(MyComponent.props)
expectType<() => {}>(MyComponent.provide)
// @ts-expect-error
expectError<[]>(MyComponent.provide)
expectType<() => null>(MyComponent.render)
expectType<Function | undefined>(defineComponent({}).render)
// Test TSX // Test TSX
expectType<JSX.Element>( expectType<JSX.Element>(
<MyComponent <MyComponent
@ -1175,3 +1163,24 @@ export default {
} }
}) })
} }
// code generated by tsc / vue-tsc, make sure this continues to work
// so we don't accidentally change the args order of DefineComponent
declare const MyButton: import('vue').DefineComponent<
{},
() => JSX.Element,
{},
{},
{},
import('vue').ComponentOptionsMixin,
import('vue').ComponentOptionsMixin,
import('vue').EmitsOptions,
string,
import('vue').VNodeProps &
import('vue').AllowedComponentProps &
import('vue').ComponentCustomProps,
Readonly<import('vue').ExtractPropTypes<{}>>,
{}
>
;<MyButton class="x" />