Skip to content

Commit

Permalink
fix(core): allow passing value of any type to isSignal function (#5…
Browse files Browse the repository at this point in the history
…0035)

Unlike the current signature where the input argument must a function, this change allows an input of any type to be passed to the `isSignal` function.

PR Close #50035
  • Loading branch information
markostanimirovic authored and AndrewKushnir committed May 16, 2023
1 parent fab7f39 commit 165b8b6
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
2 changes: 1 addition & 1 deletion goldens/public-api/core/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -838,7 +838,7 @@ export interface InputDecorator {
export function isDevMode(): boolean;

// @public
export function isSignal(value: Function): value is Signal<unknown>;
export function isSignal(value: unknown): value is Signal<unknown>;

// @public
export function isStandalone(type: Type<unknown>): boolean;
Expand Down
8 changes: 5 additions & 3 deletions packages/core/src/signals/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ export type Signal<T> = (() => T)&{
};

/**
* Checks if the given `value` function is a reactive `Signal`.
* Checks if the given `value` is a reactive `Signal`.
*
* @developerPreview
*/
export function isSignal(value: Function): value is Signal<unknown> {
return (value as Signal<unknown>)[SIGNAL] !== undefined;
export function isSignal(value: unknown): value is Signal<unknown> {
return typeof value === 'function' && (value as Signal<unknown>)[SIGNAL] !== undefined;
}

/**
Expand Down
36 changes: 36 additions & 0 deletions packages/core/test/signals/is_signal_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/

import {computed, isSignal, signal} from '@angular/core/src/signals';

describe('isSignal', () => {
it('should return true for writable signal', () => {
const writableSignal = signal('Angular');
expect(isSignal(writableSignal)).toBe(true);
});

it('should return true for readonly signal', () => {
const readonlySignal = computed(() => 10);
expect(isSignal(readonlySignal)).toBe(true);
});

it('should return false for primitive', () => {
const primitive = 0;
expect(isSignal(primitive)).toBe(false);
});

it('should return false for object', () => {
const object = {name: 'Angular'};
expect(isSignal(object)).toBe(false);
});

it('should return false for function', () => {
const fn = () => {};
expect(isSignal(fn)).toBe(false);
});
});

0 comments on commit 165b8b6

Please sign in to comment.