Skip to content

Commit 7d679bd

Browse files
markostanimirovicAndrewKushnir
authored andcommitted
fix(core): allow passing value of any type to isSignal function (#50035)
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
1 parent bd4f64d commit 7d679bd

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

goldens/public-api/core/index.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -838,7 +838,7 @@ export interface InputDecorator {
838838
export function isDevMode(): boolean;
839839

840840
// @public
841-
export function isSignal(value: Function): value is Signal<unknown>;
841+
export function isSignal(value: unknown): value is Signal<unknown>;
842842

843843
// @public
844844
export function isStandalone(type: Type<unknown>): boolean;

packages/core/src/signals/src/api.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,12 @@ export type Signal<T> = (() => T)&{
3030
};
3131

3232
/**
33-
* Checks if the given `value` function is a reactive `Signal`.
33+
* Checks if the given `value` is a reactive `Signal`.
34+
*
35+
* @developerPreview
3436
*/
35-
export function isSignal(value: Function): value is Signal<unknown> {
36-
return (value as Signal<unknown>)[SIGNAL] !== undefined;
37+
export function isSignal(value: unknown): value is Signal<unknown> {
38+
return typeof value === 'function' && (value as Signal<unknown>)[SIGNAL] !== undefined;
3739
}
3840

3941
/**
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {computed, isSignal, signal} from '@angular/core/src/signals';
10+
11+
describe('isSignal', () => {
12+
it('should return true for writable signal', () => {
13+
const writableSignal = signal('Angular');
14+
expect(isSignal(writableSignal)).toBe(true);
15+
});
16+
17+
it('should return true for readonly signal', () => {
18+
const readonlySignal = computed(() => 10);
19+
expect(isSignal(readonlySignal)).toBe(true);
20+
});
21+
22+
it('should return false for primitive', () => {
23+
const primitive = 0;
24+
expect(isSignal(primitive)).toBe(false);
25+
});
26+
27+
it('should return false for object', () => {
28+
const object = {name: 'Angular'};
29+
expect(isSignal(object)).toBe(false);
30+
});
31+
32+
it('should return false for function', () => {
33+
const fn = () => {};
34+
expect(isSignal(fn)).toBe(false);
35+
});
36+
});

0 commit comments

Comments
 (0)