Created
June 1, 2025 15:15
-
-
Save a1300/eeb5b3978b444f9819e269263a416d0c to your computer and use it in GitHub Desktop.
svg
``
const [selection, setSelection] = useState({ state: {}, items: [] });
const table = useReactTable({
data,
columns,
getRowId: row => row.id,
state: { rowSelection: selection.state },
onRowSelectionChange: updater => {
const newState = updater(selection.state);
const selectedItems = data.filter(item => newState[item.id]);
setSelection({ state: newState, items: selectedItems });
},
});
type OnlyArrays<T> = {
[K in keyof T]: T[K] extends any[] ? K : never
}[keyof T]
type ArrayItemType<T> = T extends (infer U)[] ? U : never;
function access<
T extends object,
K extends OnlyArrays<T>,
A extends (a: ArrayItemType<T[K]>) => boolean
>
(first: T, key: K, func: A): {
added: Array<ArrayItemType<T[K]>>
} {
const added = (first[key] as Array<ArrayItemType<T[K]>>).filter(func)
return {
added,
}
}
access(obj, "addresses", (a) => true);
improvement
function access<
T extends Record<K, any[]>,
K extends keyof T,
A extends (a: ArrayItemType<T[K]>) => boolean
>(
first: T,
key: K,
func: A
): {
added: ArrayItemType<T[K]>[]
} {
const added = first[key].filter(func);
return { added };
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://www.youtube.com/watch?v=HEAZDiOEhAg