Skip to content

refactor: improve splitProps type for arbitrary rest args length #930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 14 additions & 47 deletions packages/solid/src/render/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,53 +117,20 @@ export function mergeProps(...sources: any): any {
);
}

export function splitProps<T extends object, K1 extends keyof T>(
props: T,
...keys: [K1[]]
): [Pick<T, K1>, Omit<T, K1>];
export function splitProps<T extends object, K1 extends keyof T, K2 extends keyof T>(
props: T,
...keys: [K1[], K2[]]
): [Pick<T, K1>, Pick<T, K2>, Omit<T, K1 | K2>];
export function splitProps<
T extends object,
K1 extends keyof T,
K2 extends keyof T,
K3 extends keyof T
>(
props: T,
...keys: [K1[], K2[], K3[]]
): [Pick<T, K1>, Pick<T, K2>, Pick<T, K3>, Omit<T, K1 | K2 | K3>];
export function splitProps<
T extends object,
K1 extends keyof T,
K2 extends keyof T,
K3 extends keyof T,
K4 extends keyof T
>(
props: T,
...keys: [K1[], K2[], K3[], K4[]]
): [Pick<T, K1>, Pick<T, K2>, Pick<T, K3>, Pick<T, K4>, Omit<T, K1 | K2 | K3 | K4>];
export function splitProps<
T extends object,
K1 extends keyof T,
K2 extends keyof T,
K3 extends keyof T,
K4 extends keyof T,
K5 extends keyof T
>(
props: T,
...keys: [K1[], K2[], K3[], K4[], K5[]]
): [
Pick<T, K1>,
Pick<T, K2>,
Pick<T, K3>,
Pick<T, K4>,
Pick<T, K5>,
Omit<T, K1 | K2 | K3 | K4 | K5>
type SplitProps<T, K extends (readonly (keyof T)[])[]> = [
...{
[P in keyof K]: P extends `${number}`
? Pick<T, Extract<K[P], readonly (keyof T)[]>[number]>
: K[P];
},
Omit<T, K[number][number]>
];
export function splitProps<T>(props: T, ...keys: Array<(keyof T)[]>) {
const blocked = new Set(keys.flat());

export function splitProps<T, K extends [readonly (keyof T)[], ...(readonly (keyof T)[])[]]>(
props: T,
...keys: K
): SplitProps<T, K> {
const blocked = new Set<keyof T>(keys.flat());
const descriptors = Object.getOwnPropertyDescriptors(props);
const res = keys.map(k => {
const clone = {};
Expand Down Expand Up @@ -202,7 +169,7 @@ export function splitProps<T>(props: T, ...keys: Array<(keyof T)[]>) {
propTraps
)
);
return res;
return res as SplitProps<T, K>;
}

// lazy load a function component asynchronously
Expand Down