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