Sorry if I'm bothering, but I sometimes encounter ...
# javascript
h
Sorry if I'm bothering, but I sometimes encounter these "double types". For example TanStack Table has the
Updater
which is defined like this:
Copy code
sealed external interface Updater<T> /* T | ((old: T) -> T) */

inline fun <T> Updater(
    source: T,
): Updater<T> =
    unsafeSpecialCast(source)

inline fun <T> Updater(
    noinline source: (old: T) -> T,
): Updater<T> =
    unsafeSpecialCast(source)
I know I can create these using
functionalUpdate()
but how do I consume them? Currently I do
Copy code
this.onSortingChange = { newSorting ->
    setSorting(newSorting.unsafeCast<SortingState>())
}
but it feels kind of wrong. What is correct way to consume such an
Updater
?
e
Copy code
this.onSortingChange = Updater<SortingState> { newSorting ->
    setSorting(newSorting)
}
Does this work?
h
Sadly, no:
The compiler suggestion 😅
Okay this works, but it still doesn't look right this mimics the doc though (https://tanstack.com/table/v8/docs/guide/pagination):
Copy code
useReactTable<T>(
    options = jso {
        val (pagination, setPagination) = states.pagination // StateInstance<PaginationState>
        
        this.state = jso<TableState> {
            this.pagination = pagination
        }
        
        this.onPaginationChange = unsafeSpecialCast(setPagination) 
    }
)
t
We have invalid declaration for
Updater
in fact
If you use Kotlin Wrappers and you need
unsafeCast
or
asDynamic
: 1. Probably you need it without reason, please check one more time 2. It can be typization error in wrappers (current case)
@Hildebrandt Tobias could you please report the problem, I will fix it 😎
h
In YouTrack or Kotlin-Wrapper Issues?
t
Kotlin Wrappers please
h
Will do
t
Fix released in Kotlin Wrappers
2025.5.0
😎
😎 1
h
Hey sorry to come back to this. The
OnChangeFn
works. (Thanks again!) But I wonder how I handle these "double types" in general. They do come up often. For example the
Updater
from
onPaginationChange
in TanStackTable can be
(T) -> T
or just
T
. I found
isExternalObject(a, b)
but it's internal so I can't use it. I could also do
Copy code
jsTypeOf(updater) == "function")
but is this really the intended way?
I guess this works, but is it meant to be that way?
Copy code
onPaginationChange = OnChangeFn { updaterOrValue: Updater<PaginationState> ->
    val newValue: PaginationState = if (jsTypeOf(updaterOrValue) == "function") {
        val fn = updaterOrValue.unsafeCast<(PaginationState) -> PaginationState>()
        fn(pagination)
    } else {
        updaterOrValue.unsafeCast<PaginationState>()
    }
    println("New value: ${JSON.stringify(newValue)}")
},
t
Only in case when you had no frameworks
Every framework (React in our case) should provide fine adapter.
h
I don't quite understand. Is there such an adapter in the react wrappers? Also is there a way to access the
typeCheckUtils
? There seem to be some really nice tools for edge cases like
JsEqEqEq
.
t
I don't quite understand.
Is there such an adapter in the react wrappers?
In
kotlin-tanstack-react-table
Also is there a way to access the
typeCheckUtils
?
There seem to be some really nice tools for edge cases like
JsEqEqEq
.
Is there any links with examples?
h
About
kotlin-tanstack-react-table
isn't that this here, what I already use?:
t
In React part (of TanStack Table) we provide adapters for React types
h
Maybe to clarify, I already do this everywhere else:
Copy code
onSortingChange = OnChangeFn(setSorting),
onGroupingChange = OnChangeFn(setGrouping),
onColumnVisibilityChange = OnChangeFn(setColumnVisibility),
onGlobalFilterChange = OnChangeFn(setGlobalFilter),
onColumnFiltersChange = OnChangeFn(setColumnFilters),
And it works fine, no problems. But it doesn't let me do what I want in the onSomething function. What if I don't want to just pass a StateSetter? What if I want to do something beforehand? Or manipulate the value?
If the only functionality I can cover with
onPaginationChange
is passing a StateSetter. Isn't it actually a
setPaginationStateInstance
instead? And not "do something when pagination changes".