Skip to content

Commit

Permalink
fix(signal-slice): Do not allow optional properties in signalSlice (#177
Browse files Browse the repository at this point in the history
)
  • Loading branch information
palexcast authored Nov 30, 2023
1 parent 96d5de4 commit 84ccf3c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
7 changes: 7 additions & 0 deletions libs/ngxtension/signal-slice/src/signal-slice.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ describe(signalSlice.name, () => {
it('should create default selectors', () => {
expect(state.age()).toEqual(initialState.age);
});

it('should not accept optional properties in initial state', () => {
// @ts-expect-error: Testing that signalSlice should not accept an optional property in its initial state
signalSlice<{ optional?: string }, any, any, any>({
initialState: { optional: 'test' },
});
});
});

describe('sources', () => {
Expand Down
8 changes: 6 additions & 2 deletions libs/ngxtension/signal-slice/src/signal-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ import { takeUntilDestroyed, toObservable } from '@angular/core/rxjs-interop';
import { connect, type PartialOrValue, type Reducer } from 'ngxtension/connect';
import { Subject, isObservable, take, type Observable } from 'rxjs';

type NoOptionalProperties<T> = {
[P in keyof T]-?: T[P];
};

type NamedActionSources<TSignalValue> = {
[actionName: string]:
| Subject<any>
Expand Down Expand Up @@ -86,7 +90,7 @@ type ActionStreams<
export type Source<TSignalValue> = Observable<PartialOrValue<TSignalValue>>;

export type SignalSlice<
TSignalValue,
TSignalValue extends NoOptionalProperties<TSignalValue>,
TActionSources extends NamedActionSources<TSignalValue>,
TSelectors extends NamedSelectors,
TEffects extends NamedEffects
Expand All @@ -98,7 +102,7 @@ export type SignalSlice<
ActionStreams<TSignalValue, TActionSources>;

export function signalSlice<
TSignalValue,
TSignalValue extends NoOptionalProperties<TSignalValue>,
TActionSources extends NamedActionSources<TSignalValue>,
TSelectors extends NamedSelectors,
TEffects extends NamedEffects
Expand Down

0 comments on commit 84ccf3c

Please sign in to comment.