Skip to content
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

fix(signal-slice): prevent early inference in selector typing #214

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions libs/ngxtension/signal-slice/src/signal-slice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,47 @@ describe(signalSlice.name, () => {
expect(state.doubleAge()).toEqual(state.age() * 2);
});
});

it('all types should work with selectors defined', () => {
TestBed.runInInjectionContext(() => {
const testFn = jest.fn();
const state = signalSlice({
initialState,
selectors: (state) => ({
someSelector: () => state.age() * 2,
}),
actionSources: {
someActionSource: (state, $: Observable<void>) =>
$.pipe(map(() => ({ age: state().age }))),
someOtherActionSource: (state, $: Observable<void>) =>
$.pipe(
map(() => {
testFn();
return {};
}),
),
},
actionEffects: (state) => ({
someActionSource: () => {
state.age();
state.someSelector();
state.someOtherActionSource();
},
}),
effects: (state) => ({
someEffect: () => {
state.age();
state.someSelector();
state.someActionSource();
},
}),
});

state.someActionSource();

expect(testFn).toHaveBeenCalled();
});
});
});

describe('actionEffects', () => {
Expand Down
37 changes: 14 additions & 23 deletions libs/ngxtension/signal-slice/src/signal-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,17 @@ export type SignalSlice<
ActionMethods<TSignalValue, TActionSources> &
ActionStreams<TSignalValue, TActionSources>;

type SelectorsState<TSignalValue extends NoOptionalProperties<TSignalValue>> =
Signal<TSignalValue> & Selectors<TSignalValue>;

type EffectsState<
TSignalValue extends NoOptionalProperties<TSignalValue>,
TActionSources extends NamedActionSources<TSignalValue>,
TSelectors extends NamedSelectors,
> = SelectorsState<TSignalValue> &
ExtraSelectors<TSelectors> &
ActionMethods<TSignalValue, TActionSources>;

export function signalSlice<
TSignalValue extends NoOptionalProperties<TSignalValue>,
TActionSources extends NamedActionSources<TSignalValue>,
Expand All @@ -143,32 +154,12 @@ export function signalSlice<
sources?: SourceConfig<TSignalValue>;
lazySources?: SourceConfig<TSignalValue>;
actionSources?: TActionSources;
selectors?: (
state: SignalSlice<
TSignalValue,
TActionSources,
any,
TEffects,
TActionEffects
>,
) => TSelectors;
selectors?: (state: SelectorsState<TSignalValue>) => TSelectors;
effects?: (
state: SignalSlice<
TSignalValue,
TActionSources,
TSelectors,
any,
TActionEffects
>,
state: EffectsState<TSignalValue, TActionSources, TSelectors>,
) => TEffects;
actionEffects?: (
state: SignalSlice<
TSignalValue,
TActionSources,
TSelectors,
any,
TActionEffects
>,
state: EffectsState<TSignalValue, TActionSources, TSelectors>,
) => TActionEffects;
}): SignalSlice<
TSignalValue,
Expand Down