angularangular-signals

Angular 'Signal' only refers to a type, but is being used as a value here.ts(2693)


I'm using angular version 17.2.0. And in the service

import { Injectable } from '@angular/core';
import { Signal, ReadOnlySignal } from '@angular/core';

@Injectable({
  providedIn: 'root' // Make the service available throughout the application
})
export class CurrentTimeService {
  private currentTimeInSeconds = new Signal<number>(); // throw Error in this line.
}
}

I want to create a service in the latest angular version 17.2.0 for getting currentTime based on in Angular signal


Solution

  • As the error says, we need to define the signal with a small first letter signal instead of Signal(type). Also we don't need to use new!

    Angular signal documentation

    Signal tutorial

    import { Injectable, signal } from '@angular/core';
    import { Signal } from '@angular/core';
    
    @Injectable({
      providedIn: 'root',
    })
    export class CurrentTimeService {
      private currentTimeInSeconds = signal<number>(0);
    
      constructor() {}
    }