Skip to content

fix: improve typing on makeStateUpdater function #5878

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions packages/table-core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { TableOptionsResolved, TableState, Updater } from './types'
import { RowData, Table, TableOptionsResolved, TableState, Updater } from './types'

export type PartialKeys<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>
export type RequiredKeys<T, K extends keyof T> = Omit<T, K> &
Expand Down Expand Up @@ -77,26 +77,29 @@ export type NoInfer<T> = [T][T extends any ? 0 : never]
export type Getter<TValue> = <TTValue = TValue>() => NoInfer<TTValue>

///
function updaterIsFunction<T>(updater: Updater<T>): updater is ((old: T) => T) {
return typeof updater === 'function'
}

export function functionalUpdate<T>(updater: Updater<T>, input: T): T {
return typeof updater === 'function'
? (updater as (input: T) => T)(input)
return updaterIsFunction(updater)
? updater(input)
: updater
}

export function noop() {
//
}

export function makeStateUpdater<K extends keyof TableState>(
export function makeStateUpdater<K extends keyof TableState, TData extends RowData>(
key: K,
instance: unknown
instance: Table<TData>
) {
return (updater: Updater<TableState[K]>) => {
;(instance as any).setState(<TTableState>(old: TTableState) => {
instance.setState((old: TableState): TableState => {
return {
...old,
[key]: functionalUpdate(updater, (old as any)[key]),
[key]: functionalUpdate(updater, old[key]),
}
})
}
Expand Down