Skip to content

Commit

Permalink
feat(signals): allow defining private SignalStore members (#4447)
Browse files Browse the repository at this point in the history
Closes #4443
  • Loading branch information
markostanimirovic authored Jul 23, 2024
1 parent e2ab916 commit 1858a51
Show file tree
Hide file tree
Showing 7 changed files with 252 additions and 73 deletions.
52 changes: 52 additions & 0 deletions modules/signals/spec/types/signal-store.types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,58 @@ describe('signalStore', () => {
expectSnippet(snippet + `store.log(10);`).toFail();
});

it('omits private store members from the public instance', () => {
const snippet = `
const CounterStore = signalStore(
withState({ count1: 0, _count2: 0 }),
withComputed(({ count1, _count2 }) => ({
_doubleCount1: computed(() => count1() * 2),
doubleCount2: computed(() => _count2() * 2),
})),
withMethods(() => ({
increment1() {},
_increment2() {},
})),
withHooks({
onInit({ increment1, _increment2 }) {
increment1();
_increment2();
},
})
);
const store = new CounterStore();
`;

expectSnippet(snippet).toSucceed();

expectSnippet(snippet).toInfer(
'store',
'{ count1: Signal<number>; doubleCount2: Signal<number>; increment1: () => void; } & StateSource<{ count1: number; }>'
);
});

it('prevents private state slices from being updated from the outside', () => {
const snippet = `
const CounterStore = signalStore(
{ protectedState: false },
withState({ count1: 0, _count2: 0 }),
);
const store = new CounterStore();
`;

expectSnippet(`
${snippet}
patchState(store, { count1: 1 });
`).toSucceed();

expectSnippet(`
${snippet}
patchState(store, { count1: 1, _count2: 1 });
`).toFail(/'_count2' does not exist in type/);
});

describe('custom features', () => {
const baseSnippet = `
function withX() {
Expand Down
Loading

0 comments on commit 1858a51

Please sign in to comment.