Skip to content

Commit 38a354f

Browse files
SkyZeroZxkirjs
authored andcommitted
docs: Adds signal type checking documentation
1 parent b6fee3f commit 38a354f

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

adev/src/content/guide/signals/overview.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,29 @@ Equality functions can be provided to both writable and computed signals.
195195

196196
HELPFUL: By default, signals use referential equality ([`Object.is()`](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/Object/is) comparison).
197197

198+
### Type checking signals
199+
200+
You can use `isSignal` to check if a value is a `Signal`:
201+
202+
```ts
203+
const count = signal(0);
204+
const doubled = computed(() => count() * 2);
205+
206+
isSignal(count); // true
207+
isSignal(doubled); // true
208+
isSignal(42); // false
209+
```
210+
211+
To specifically check if a signal is writable, use `isWritableSignal`:
212+
213+
```ts
214+
const count = signal(0);
215+
const doubled = computed(() => count() * 2);
216+
217+
isWritableSignal(count); // true
218+
isWritableSignal(doubled); // false
219+
```
220+
198221
### Reading without tracking dependencies
199222

200223
Rarely, you may want to execute code which may read signals within a reactive function such as `computed` or `effect` _without_ creating a dependency.

packages/core/src/render3/reactivity/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import type {WritableSignal} from './signal';
1717
*
1818
* Ordinary values can be turned into `Signal`s with the `signal` function.
1919
*
20+
* @see [What are signals?](guide/signals#what-are-signals)
21+
*
2022
* @publicApi 17.0
2123
*/
2224
export type Signal<T> = (() => T) & {
@@ -26,6 +28,8 @@ export type Signal<T> = (() => T) & {
2628
/**
2729
* Checks if the given `value` is a reactive `Signal`.
2830
*
31+
* @see [Type checking signals](guide/signals#type-checking-signals)
32+
*
2933
* @publicApi 17.0
3034
*/
3135
export function isSignal(value: unknown): value is Signal<unknown> {
@@ -35,13 +39,17 @@ export function isSignal(value: unknown): value is Signal<unknown> {
3539
/**
3640
* A comparison function which can determine if two values are equal.
3741
*
42+
* @see [Signal equality functions](guide/signals#signal-equality-functions)
43+
*
3844
* @publicApi 17.0
3945
*/
4046
export type ValueEqualityFn<T> = (a: T, b: T) => boolean;
4147

4248
/**
4349
* Checks if the given `value` is a writeable signal.
4450
*
51+
* @see [Type checking signals](guide/signals#type-checking-signals)
52+
*
4553
* @publicApi 21.1
4654
*/
4755
export function isWritableSignal(value: unknown): value is WritableSignal<unknown> {

0 commit comments

Comments
 (0)