Skip to content

TS Updater "not callable" #4364

@Patrick-Ullrich

Description

@Patrick-Ullrich

Describe the bug

onRowSelectionChange, onSortingChange, onExpandedChange, etc are all expecting a OnChangeFn<T>:

declare type Updater<T> = T | ((old: T) => T);
declare type OnChangeFn<T> = (updaterOrValue: Updater<T>) => void;

Usage works fine when passing in a setState method, but I am struggling to enhance it because typescript believes it's a Value and not a function.

e.g.

// ✅ TS is happy with setSorting 
const [sorting, setSorting] = useState<SortingState>([]);
// ✅ TS is happy with handleMultiColumnSortconst 
handleMultiColumnSort = (state: Updater<SortingState>) => {
    // ❌ When logging state we see it is a function
    // when trying to call it though, typescript throws an error:
    console.log('state', state?.());
};

Typescript error:

This expression is not callable.
  Not all constituents of type 'Updater<SortingState>' are callable.
    Type 'SortingState' has no call signatures.ts(2349)

Your minimal, reproducible example

https://codesandbox.io/s/xenodochial-herschel-zlq6y9?file=/src/main.tsx

Steps to reproduce

  1. Go to sandbox and open the console in codesandbox
  2. Click on any header, notice the function and actual object logs
  3. Go to ln 91+ to see the issue

Expected behavior

If state is a function, typescript should show the correct type

How often does this bug happen?

No response

Screenshots or Videos

No response

Platform

OS: macOS

react-table version

8.5.31

TypeScript version

v4.6.3

Additional context

No response

Terms & Code of Conduct

  • I agree to follow this project's Code of Conduct
  • I understand that if my bug cannot be reliable reproduced in a debuggable environment, it will probably not be fixed and this issue may even be closed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions