pending/success/error pattern with jotai -- conditionally check whether .prop(...)
resolves to undefined or not
#2551
-
Im attempting to implement the pending/success/error pattern with jotai. I am struggling to create a derived atom for the export type Async<T> = Pending | Success<T> | Error;
type Pending = {
loadingStatus: 'pending';
data: undefined;
error: undefined;
};
type Success<T> = {
loadingStatus: 'success';
data: T;
error: undefined;
};
type Error = {
loadingStatus: 'error';
data: undefined;
error: any;
};
interface Data {
outer: {
a: 1,
b: 2,
inner: {
deeplyNested: true
}
},
bar: true
}
export const userDataAtom = atomWithImmer<Async<Data>>({
loadingStatus: 'pending',
data: undefined,
error: undefined,
})
// Goal: create a derived atom for the attribute `deeplyNested`
const x = focusAtom(userDataAtom, optic => {
return optic.prop('data').prop('outer').prop('inner');
// ^^^^^^^--Error: argument of type 'string' is not assignable to parameter of type 'never'
}); Is there a way to conditionally check whether I would like to return |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Just add return optic.prop('data').optional().prop('outer').prop('inner'); |
Beta Was this translation helpful? Give feedback.
Just add
optional()
: