Skip to content

Commit

Permalink
chore(signals): add default undefined type (#578)
Browse files Browse the repository at this point in the history
* add default undefined type

* add changeset
  • Loading branch information
JoviDeCroock authored Jun 26, 2024
1 parent 5a02bfa commit 931404e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions .changeset/new-emus-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@preact/signals": minor
"@preact/signals-react": minor
"@preact/signals-core": minor
---

Allow for passing no argument to the signal and the type to be automatically inferred as `T | undefined`
6 changes: 4 additions & 2 deletions packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,9 @@ Object.defineProperty(Signal.prototype, "value", {
* @param value The initial value for the signal.
* @returns A new signal.
*/
function signal<T>(value: T): Signal<T> {
export function signal<T>(value: T): Signal<T>;
export function signal<T = undefined>(): Signal<T | undefined>;
export function signal<T>(value?: T): Signal<T> {
return new Signal(value);
}

Expand Down Expand Up @@ -821,4 +823,4 @@ function effect(fn: EffectFn): () => void {
return effect._dispose.bind(effect);
}

export { signal, computed, effect, batch, untracked, Signal, ReadonlySignal };
export { computed, effect, batch, untracked, Signal, ReadonlySignal };
6 changes: 4 additions & 2 deletions packages/preact/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,10 @@ Component.prototype.shouldComponentUpdate = function (
return false;
};

export function useSignal<T>(value: T) {
return useMemo(() => signal<T>(value), []);
export function useSignal<T>(value: T): Signal<T>;
export function useSignal<T = undefined>(): Signal<T | undefined>;
export function useSignal<T>(value?: T) {
return useMemo(() => signal<T | undefined>(value), []);
}

export function useComputed<T>(compute: () => T) {
Expand Down
6 changes: 4 additions & 2 deletions packages/react/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,10 @@ export function useSignals(usage?: EffectStoreUsage): EffectStore {
return _useSignalsImplementation(usage);
}

export function useSignal<T>(value: T): Signal<T> {
return useMemo(() => signal<T>(value), Empty);
export function useSignal<T>(value: T): Signal<T>;
export function useSignal<T = undefined>(): Signal<T | undefined>;
export function useSignal<T>(value?: T) {
return useMemo(() => signal<T | undefined>(value), Empty);
}

export function useComputed<T>(compute: () => T): ReadonlySignal<T> {
Expand Down

0 comments on commit 931404e

Please sign in to comment.