Replies: 1 comment
-
|
There is benefit to typing things this way. But you're not getting any of it here. The const injectRetainPrevious = <
G extends AnyNodeGenerics<{
State: { data: any; isLoading: boolean };
}>
>(
signal: ZeduxNode<G & { Params: any; Template: any }>
): Signal<Pick<G, "Events" | "State">> => { ... }Then callers must pass nodes with the expected state shape and the function's own Since atoms are signals, you can also do this with the Signal class instead of ZeduxNode - and that sounds like what you want here - however that requires some manual type intersections You discovered the types for a Signal's generics - type SignalGenerics = NodeGenerics & { Params?: any; Template?: any };
type AnySignalGenerics<
G extends Partial<SignalGenerics> = { [K in keyof SignalGenerics]: any }
> = {
[K in keyof SignalGenerics]: K extends keyof G ? G[K] : any;
};With that defined, you can use the Signal class instead of ZeduxNode: const injectRetainPrevious = <
G extends SignalGenerics & {
State: { data: any; isLoading: boolean } & Partial<Record<string, any>>;
}
>(
signal: Signal<G>
): Signal<Pick<G, "Events" | "State">> => { ... }Note the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I was having trouble figuring out a type for a helper like the one below, that should be able to take a signal or an atom instance, but I ended up finded the
handleStateChangefunction and following its example. Since Signal is actually a subclass of of ZeduxNode, would it make sense to have ZeduxNode implement an interface or have a helper type that shares these operations for easy typing, or with more usage would I find there's good reason to require passing the type variables and bounds in this way?Beta Was this translation helpful? Give feedback.
All reactions