Skip to content

Commit

Permalink
fix: typeCallConfig not generating correct types for resultProp
Browse files Browse the repository at this point in the history
fix #70
  • Loading branch information
Gabriel Guerrero authored and gabrielguerrero committed May 15, 2024
1 parent bf3d6f9 commit 5d49962
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
34 changes: 34 additions & 0 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,5 +143,39 @@ describe('withCalls', () => {
expect(store.foo()).toBe('test');
});
});

it('check typedCallConfig with resultProp generates custom prop name ', async () => {
TestBed.runInInjectionContext(() => {
const Store = signalStore(
withState({ foo: 'bar' }),
withCalls((store) => ({
testCall: typedCallConfig({
call: ({ ok }: { ok: boolean }) => {
return ok
? apiResponse
: apiResponse.pipe(
tap(() => throwError(() => new Error('fail'))),
);
},
resultProp: 'baz',
onSuccess: (result) => {
// patchState should be able to update the store inside onSuccess
patchState(store, { foo: result });
},
onError,
}),
})),
);
const store = new Store();
expect(store.isTestCallLoading()).toBeFalsy();
store.testCall({ ok: true });
expect(store.isTestCallLoading()).toBeTruthy();
apiResponse.next('test');
expect(store.isTestCallLoaded()).toBeTruthy();
expect((store as any).testCallResult).toBeUndefined();
expect(store.foo()).toBe('test');
expect(store.baz()).toBe('test');
});
});
});
});
17 changes: 12 additions & 5 deletions libs/ngrx-traits/signals/src/lib/with-calls/with-calls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ export function withCalls<
) => Calls,
): SignalStoreFeature<
Input,
Input & {
{
state: NamedCallStatusState<keyof Calls & string> & {
[K in keyof Calls as Calls[K] extends CallConfig
? Calls[K]['storeResult'] extends false
? never
: Calls[K]['resultProp'] extends string
? Calls[K]['resultProp']
: Calls[K]['resultProp'] extends string | undefined
? Calls[K]['resultProp'] & string
: `${K & string}Result`
: `${K & string}Result`]: ExtractCallResultType<Calls[K]> | undefined;
};
Expand Down Expand Up @@ -286,6 +286,13 @@ export function typedCallConfig<
Params extends readonly any[] = any[],
Result = any,
PropName extends string = string,
>(config: CallConfig<Params, Result, PropName>) {
return config;
C extends CallConfig<Params, Result, PropName> = CallConfig<
Params,
Result,
PropName
>,
>(config: CallConfig<Params, Result, PropName>): C {
// this fixes weird issue where typedCallConfig was not generating the right types
// when CallConfig resultProp was defined
return { ...config } as C;
}
1 change: 1 addition & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"moduleResolution": "node",
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"noErrorTruncation": true,
"importHelpers": true,
"target": "es2015",
"module": "esnext",
Expand Down

0 comments on commit 5d49962

Please sign in to comment.