Skip to content

signals: TypeScript and nullability #49161

@cexbrayat

Description

@cexbrayat

Which @angular/* package(s) are the source of the bug?

core, Don't known / other

Is this a regression?

No

Description

The current implementation of signals uses a function to access the value, and TypeScript has issues with type guards in that case:

const count = signal(null as null | number);
const total: number = count() !== null ? count() : 0;
// Error: Type `number | null` is not assignable to type `number`

This can be workaround by storing the value in a temporary variable or with an explicit cast, but this could be a bit cumbersome, as I imagine the same would happen with ngIf in templates:

const count = signal(null as null | number);
const value = count();
const total: number = value !== null ? value : 0;
// Works

Please provide a link to a minimal reproduction of the bug

No response

Please provide the exception or error you saw

No response

Please provide the environment you discovered this bug in (run ng version)

No response

Anything else?

No response

Metadata

Metadata

Assignees

Type

No type

Projects

Status

In Progress

Relationships

None yet

Development

No branches or pull requests

Issue actions