mirror of https://github.com/vuejs/core.git
chore: improvements on resolving props
This commit is contained in:
parent
5890e8c72a
commit
bdc1d7371d
|
@ -13,7 +13,6 @@ import {
|
||||||
type DeclareEmits,
|
type DeclareEmits,
|
||||||
type DynamicComponent,
|
type DynamicComponent,
|
||||||
type ExtractComponentOptions,
|
type ExtractComponentOptions,
|
||||||
type ExtractComponentPropOptions,
|
|
||||||
type FunctionalComponent,
|
type FunctionalComponent,
|
||||||
type ObjectToComponentProps,
|
type ObjectToComponentProps,
|
||||||
type PropType,
|
type PropType,
|
||||||
|
@ -21,6 +20,7 @@ import {
|
||||||
type SlotsType,
|
type SlotsType,
|
||||||
defineAsyncComponent,
|
defineAsyncComponent,
|
||||||
defineComponent,
|
defineComponent,
|
||||||
|
defineProps,
|
||||||
} from 'vue'
|
} from 'vue'
|
||||||
|
|
||||||
const propsOptions = {
|
const propsOptions = {
|
||||||
|
@ -314,13 +314,6 @@ describe('Component Props', () => {
|
||||||
{},
|
{},
|
||||||
typeof __options
|
typeof __options
|
||||||
>
|
>
|
||||||
|
|
||||||
const propsOptions = {} as ExtractComponentPropOptions<
|
|
||||||
typeof DeclaredComp
|
|
||||||
>
|
|
||||||
|
|
||||||
propsOptions.getFn
|
|
||||||
|
|
||||||
const props = {} as ComponentProps<typeof DeclaredComp>
|
const props = {} as ComponentProps<typeof DeclaredComp>
|
||||||
|
|
||||||
expectType<{ foo: string | undefined; getFn: (a: string) => void }>(props)
|
expectType<{ foo: string | undefined; getFn: (a: string) => void }>(props)
|
||||||
|
@ -333,6 +326,72 @@ describe('Component Props', () => {
|
||||||
// @ts-expect-error not any
|
// @ts-expect-error not any
|
||||||
expect<{ random: string }>(props)
|
expect<{ random: string }>(props)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
// usage with defineProps
|
||||||
|
const props = defineProps(['foo', 'bar'])
|
||||||
|
|
||||||
|
const __options = defineComponent({
|
||||||
|
props: ['foo', 'bar'],
|
||||||
|
setup() {
|
||||||
|
return () => {}
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const Comp = {} as DeclareComponent<
|
||||||
|
typeof props,
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
typeof __options
|
||||||
|
>
|
||||||
|
|
||||||
|
const p = {} as ComponentProps<typeof Comp>
|
||||||
|
|
||||||
|
expectType<{
|
||||||
|
foo?: any
|
||||||
|
bar?: any
|
||||||
|
}>(p)
|
||||||
|
|
||||||
|
// @ts-expect-error not any
|
||||||
|
expectType<{ random: any }>(p)
|
||||||
|
|
||||||
|
expectType<Array<'foo' | 'bar'>>(Comp.props)
|
||||||
|
}
|
||||||
|
{
|
||||||
|
// generic
|
||||||
|
const __options = defineComponent({
|
||||||
|
props: {
|
||||||
|
item: null,
|
||||||
|
getFn: Function,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
const Comp = {} as DeclareComponent<
|
||||||
|
{
|
||||||
|
new <T>(): {
|
||||||
|
$props: {
|
||||||
|
item: T
|
||||||
|
getFn: (a: T) => void
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
{},
|
||||||
|
typeof __options
|
||||||
|
>
|
||||||
|
|
||||||
|
const props = {} as ComponentProps<typeof Comp>
|
||||||
|
|
||||||
|
expectType<{
|
||||||
|
item: unknown
|
||||||
|
getFn: (a: unknown) => void
|
||||||
|
}>(props)
|
||||||
|
|
||||||
|
// @ts-expect-error not any
|
||||||
|
expectType<unknown>(props.test)
|
||||||
|
}
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -276,7 +276,9 @@ export type ComponentProps<T> = (T extends {
|
||||||
// We use the raw props to avoid losing the props type
|
// We use the raw props to avoid losing the props type
|
||||||
[RawPropsSymbol]: infer P
|
[RawPropsSymbol]: infer P
|
||||||
}
|
}
|
||||||
? P
|
? P extends { new (): { $props?: infer PP } }
|
||||||
|
? PP
|
||||||
|
: P
|
||||||
: ExtractComponentPropOptions<T> extends infer P
|
: ExtractComponentPropOptions<T> extends infer P
|
||||||
? P extends Readonly<Array<infer V>>
|
? P extends Readonly<Array<infer V>>
|
||||||
? [V] extends [string]
|
? [V] extends [string]
|
||||||
|
@ -481,6 +483,8 @@ export type DeclareComponent<
|
||||||
}
|
}
|
||||||
? ObjectToComponentProps<TProps>
|
? ObjectToComponentProps<TProps>
|
||||||
: {}
|
: {}
|
||||||
|
} & {
|
||||||
|
[RawPropsSymbol]: Props
|
||||||
}
|
}
|
||||||
: DefineComponent<
|
: DefineComponent<
|
||||||
ObjectToComponentProps<Props>,
|
ObjectToComponentProps<Props>,
|
||||||
|
|
Loading…
Reference in New Issue