Give clearer type names to dynamicComponent

This commit is contained in:
Henrik Giesel 2021-04-08 17:07:02 +02:00
parent a2d37206ea
commit bf33397855

View file

@ -6,22 +6,22 @@ export interface DynamicSvelteComponent<
component: T; component: T;
} }
export const dynamicComponent = <T extends typeof SvelteComponentDev>(component: T) => < export const dynamicComponent = <Comp extends typeof SvelteComponentDev>(component: Comp) => <
U extends NonNullable<ConstructorParameters<T>[0]["props"]>, Props extends NonNullable<ConstructorParameters<Comp>[0]["props"]>,
V extends string = never Lazy extends string = never
>( >(
props: Omit<U, V>, props: Omit<Props, Lazy>,
lazyProps: { [Property in keyof Pick<U, V>]: () => Pick<U, V>[Property] } lazyProps: { [Property in keyof Pick<Props, Lazy>]: () => Pick<Props, Lazy>[Property] }
): DynamicSvelteComponent<T> & U => { ): DynamicSvelteComponent<Comp> & Props => {
const dynamicComponent = { component, ...props }; const dynamicComponent = { component, ...props };
for (const property in lazyProps) { for (const property in lazyProps) {
const get = lazyProps[property]; const get = lazyProps[property];
const propertyDescriptor: TypedPropertyDescriptor< const propertyDescriptor: TypedPropertyDescriptor<
Pick<U, V>[Extract<keyof Pick<U, V>, string>] Pick<Props, Lazy>[Extract<keyof Pick<Props, Lazy>, string>]
> = { get, enumerable: true }; > = { get, enumerable: true };
Object.defineProperty(dynamicComponent, property, propertyDescriptor); Object.defineProperty(dynamicComponent, property, propertyDescriptor);
} }
return dynamicComponent as DynamicSvelteComponent<T> & U; return dynamicComponent as DynamicSvelteComponent<Comp> & Props;
}; };