Skip to content

Commit e11c23f

Browse files
feat(signals): allow access to methods in withComputed (#4864)
Closes #4846
1 parent cb1a2ba commit e11c23f

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

modules/signals/spec/types/with-computed.types.spec.ts

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
import { expecter } from 'ts-snippet';
22
import { compilerOptions } from './helpers';
3-
import { signalStore, withComputed } from 'modules/signals/src';
4-
import { TestBed } from '@angular/core/testing';
53

64
describe('withComputed', () => {
75
const expectSnippet = expecter(
86
(code) => `
97
import {
108
deepComputed,
9+
patchState,
1110
signalStore,
1211
withComputed,
12+
withMethods,
13+
withProps,
14+
withState,
1315
} from '@ngrx/signals';
1416
import { TestBed } from '@angular/core/testing';
1517
import { signal } from '@angular/core';
@@ -19,6 +21,49 @@ describe('withComputed', () => {
1921
compilerOptions()
2022
);
2123

24+
it('has access to props, state signals and methods', () => {
25+
const snippet = `
26+
signalStore(
27+
withState({
28+
a: 1,
29+
}),
30+
withProps(() => {
31+
return {
32+
b: 2,
33+
};
34+
}),
35+
withMethods(({ a, b }) => ({
36+
sum: () => a() + b,
37+
})),
38+
withComputed(({ a, b, sum }) => ({
39+
prettySum: () => \`Sum: \${a()} + \${b} = \${sum()}\`,
40+
}))
41+
);
42+
`;
43+
44+
expectSnippet(snippet).toSucceed();
45+
});
46+
47+
it('has no access to the state source', () => {
48+
const snippet = `
49+
signalStore(
50+
withState({
51+
a: 1,
52+
}),
53+
withComputed((store) => ({
54+
prettySum: () => {
55+
patchState(store, { a: 2 });
56+
return store.a();
57+
},
58+
}))
59+
);
60+
`;
61+
62+
expectSnippet(snippet).toFail(
63+
/not assignable to parameter of type 'WritableStateSource<object>'/
64+
);
65+
});
66+
2267
it('creates a Signal automatically', () => {
2368
const snippet = `
2469
const Store = signalStore(

modules/signals/spec/with-computed.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { computed, signal } from '@angular/core';
2+
import { TestBed } from '@angular/core/testing';
23
import {
34
deepComputed,
45
signalStoreFeature,
@@ -7,7 +8,6 @@ import {
78
withState,
89
} from '../src';
910
import { getInitialInnerStore, signalStore } from '../src/signal-store';
10-
import { TestBed } from '@angular/core/testing';
1111

1212
describe('withComputed', () => {
1313
it('adds computed signals to the store immutably', () => {

modules/signals/src/with-computed.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export function withComputed<
2828
>
2929
>(
3030
computedFactory: (
31-
store: Prettify<StateSignals<Input['state']> & Input['props']>
31+
store: Prettify<
32+
StateSignals<Input['state']> & Input['props'] & Input['methods']
33+
>
3234
) => ComputedDictionary
3335
): SignalStoreFeature<
3436
Input,

projects/ngrx.io/content/guide/signals/signal-store/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export class BookSearch {
167167

168168
Computed signals can be added to the store using the `withComputed` feature.
169169
This feature accepts a factory function as an input argument, which is executed within the injection context.
170-
The factory should return a dictionary containing either computed signals or functions that return values (which are automatically wrapped in computed signals), utilizing previously defined state signals and properties that are accessible through its input argument.
170+
The factory should return a dictionary containing either computed signals or functions that return values (which are automatically wrapped in computed signals), utilizing previously defined state signals, properties, and methods that are accessible through its input argument.
171171

172172
<code-example header="book-search-store.ts">
173173

0 commit comments

Comments
 (0)