-
-
Notifications
You must be signed in to change notification settings - Fork 2k
feat(signals): add SignalStoreFeatureType
#5186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
markostanimirovic
merged 12 commits into
main
from
signals/feat/signal-store-feature-type
Aug 6, 2026
+298
−0
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
a421716
feat(signals): add `SignalStoreFeatureType`
rainerhahnekamp 6022fb6
feat: add edge cases to tests
rainerhahnekamp 34be301
feat: support features with input
rainerhahnekamp 0f6c721
refactor: rename test file
rainerhahnekamp 5aa43a7
Update modules/signals/src/index.ts
rainerhahnekamp f7c60cb
feat: handle bare feature functions
rainerhahnekamp ae0f3e9
feat: improve docs as requested
rainerhahnekamp 1500435
feat: improve jsdoc
rainerhahnekamp bba8bc6
docs: improve docs
rainerhahnekamp 51db93e
feat: add test which checks the outcome of a whole signalStore
rainerhahnekamp 53d90d7
fix: imports in index
rainerhahnekamp 53a41fd
fix: remove type in index.ts
rainerhahnekamp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
217 changes: 217 additions & 0 deletions
217
modules/signals/spec/types/signal-store-feature-type.spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,217 @@ | ||
| import { Signal } from '@angular/core'; | ||
| import { describe, expectTypeOf, it } from 'vitest'; | ||
| import { | ||
| signalStore, | ||
| signalStoreFeature, | ||
| SignalStoreFeatureType, | ||
| type, | ||
| withMethods, | ||
| withProps, | ||
| withState, | ||
| } from '../../src'; | ||
|
|
||
| describe('SignalStoreFeatureType', () => { | ||
| function withCounter() { | ||
| return signalStoreFeature( | ||
| withState({ count: 0 }), | ||
| withMethods(() => ({ | ||
| increment(): void {}, | ||
| })) | ||
| ); | ||
| } | ||
|
|
||
| it('uses extracted output as input for another custom feature', () => { | ||
| type CounterFeature = SignalStoreFeatureType<typeof withCounter>; | ||
|
|
||
| signalStoreFeature( | ||
| type<CounterFeature>(), | ||
| withMethods((store) => { | ||
| expectTypeOf(store).toMatchObjectType<{ | ||
| count: Signal<number>; | ||
| increment: () => void; | ||
| }>(); | ||
|
|
||
| return {}; | ||
| }) | ||
| ); | ||
|
markostanimirovic marked this conversation as resolved.
|
||
| }); | ||
|
|
||
| it('extracts the output result of a custom feature', () => { | ||
| type CounterFeature = SignalStoreFeatureType<typeof withCounter>; | ||
|
|
||
| expectTypeOf<CounterFeature>().toMatchObjectType<{ | ||
| state: { count: number }; | ||
| methods: { increment: () => void }; | ||
| props: {}; | ||
| }>(); | ||
| }); | ||
|
|
||
| it('extracts output from generic feature factories', () => { | ||
| function withContainer<T>(initialValue: T) { | ||
| return signalStoreFeature(withProps(() => ({ a: initialValue }))); | ||
| } | ||
|
|
||
| type ContainerType = SignalStoreFeatureType<typeof withContainer<number>>; | ||
|
|
||
| expectTypeOf<ContainerType>().toMatchObjectType<{ | ||
| state: {}; | ||
| methods: {}; | ||
| props: { a: number }; | ||
| }>(); | ||
| }); | ||
|
|
||
| it('preserves required input from custom features', () => { | ||
| function withCounterLogger() { | ||
| return signalStoreFeature( | ||
| { | ||
| state: type<{ count: number }>(), | ||
| methods: type<{ increment: () => void }>(), | ||
| }, | ||
| withMethods(({ count, increment }) => ({ | ||
| logAndIncrement(): void { | ||
| console.log(count()); | ||
| increment(); | ||
| }, | ||
| })) | ||
| ); | ||
| } | ||
|
|
||
| type CounterLoggerFeature = SignalStoreFeatureType< | ||
| typeof withCounterLogger | ||
| >; | ||
|
|
||
| expectTypeOf<CounterLoggerFeature>().toMatchObjectType<{ | ||
| state: { count: number }; | ||
| methods: { | ||
| increment: () => void; | ||
| logAndIncrement: () => void; | ||
| }; | ||
| props: {}; | ||
| }>(); | ||
|
|
||
| signalStoreFeature( | ||
| type<CounterLoggerFeature>(), | ||
| withMethods((store) => { | ||
| expectTypeOf(store).toMatchObjectType<{ | ||
| count: Signal<number>; | ||
| increment: () => void; | ||
| logAndIncrement: () => void; | ||
| }>(); | ||
|
|
||
| return {}; | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| describe('intersections', () => { | ||
| function withContainer<T>(initialValue: T) { | ||
| return signalStoreFeature(withProps(() => ({ a: initialValue }))); | ||
| } | ||
|
|
||
| function withCounter() { | ||
| return signalStoreFeature( | ||
| withState({ count: 0 }), | ||
| withMethods(() => ({ | ||
| increment(): void {}, | ||
| })) | ||
| ); | ||
| } | ||
|
|
||
| type CounterContainerFeature = SignalStoreFeatureType<typeof withCounter> & | ||
| SignalStoreFeatureType<typeof withContainer<string>>; | ||
|
|
||
| it('preserves state, props, and methods from intersected feature outputs', () => { | ||
| expectTypeOf<CounterContainerFeature>().toMatchObjectType<{ | ||
| state: { | ||
| count: number; | ||
| }; | ||
| methods: { | ||
| increment: () => void; | ||
| }; | ||
| props: { | ||
| a: string; | ||
| }; | ||
| }>(); | ||
| }); | ||
|
|
||
| it('uses intersected feature outputs as input for another custom feature', () => { | ||
| signalStoreFeature( | ||
| type<CounterContainerFeature>(), | ||
| withMethods((store) => { | ||
| expectTypeOf(store).toMatchObjectType<{ | ||
| count: Signal<number>; | ||
| increment: () => void; | ||
| a: string; | ||
| }>(); | ||
|
|
||
| return {}; | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('uses inline', () => { | ||
| signalStoreFeature( | ||
| type<CounterContainerFeature & { state: { value: number } }>(), | ||
| withMethods((store) => { | ||
| expectTypeOf(store).toMatchObjectType<{ | ||
| count: Signal<number>; | ||
| value: Signal<number>; | ||
| increment: () => void; | ||
| a: string; | ||
| }>(); | ||
|
|
||
| return {}; | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('intersects on the same member, which results in a never', () => { | ||
| signalStoreFeature( | ||
| type<CounterContainerFeature & { state: { count: string } }>(), | ||
| withMethods((store) => { | ||
| expectTypeOf(store.count).toEqualTypeOf<Signal<never>>(); | ||
|
|
||
| return {}; | ||
| }) | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| it('ignores unresolved input from bare feature factories', () => { | ||
| function withLogger() { | ||
| return withMethods(() => ({ log(): void {} })); | ||
| } | ||
|
|
||
| signalStoreFeature( | ||
| type<SignalStoreFeatureType<typeof withLogger>>(), | ||
| withMethods((store) => { | ||
| expectTypeOf(store.log).toEqualTypeOf<() => void>(); | ||
| // @ts-expect-error no Function index signature | ||
| store.anythingAtAll(); | ||
|
|
||
| return {}; | ||
| }) | ||
| ); | ||
| }); | ||
|
|
||
| it('does a full check on the `signalStore` outcome', () => { | ||
| function withCounterLogger() { | ||
| return signalStoreFeature( | ||
| type<SignalStoreFeatureType<typeof withCounter>>(), | ||
| withMethods(({ count, increment }) => ({ | ||
| logAndIncrement(): void { | ||
| increment(); | ||
| }, | ||
| })) | ||
| ); | ||
| } | ||
|
|
||
| const CounterStore = signalStore(withCounter(), withCounterLogger()); | ||
|
|
||
| expectTypeOf<InstanceType<typeof CounterStore>>().toMatchObjectType<{ | ||
| count: Signal<number>; | ||
| increment: () => void; | ||
| logAndIncrement: () => void; | ||
| }>(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.