0

I have a contact service that is responsible for managing contacts, in a address book, type of way. for example my interface looks like this:

export interface AddressBook {
  a?: Contact[];
  //...
}

and Contact[] is just stuff like name, age, email. In my service I currently have this implementation:

  private addressBook: Map<string, Contact[]> = new Map<string, Contact[]>();
  private readonly ab$: BehaviorSubject<string | Contact[]> = new BehaviorSubject<string | Contact[]>(this.addressBook);

  public constructor(){}

  public getContacts(): Observable<string | Contact[]> {
    return this.ab$.asObservable();
  }

however the problem that I am getting is with the private readonly ab$: BehaviorSubject<string | Contact[]> = new BehaviorSubject<string | Contact[]>(this.addressBook) where it says:

not assignable to parameter of type 'string | Contact[]'. Type 'Map' is missing the following properties from type 'Contact[]': length, pop, push, concat, and 21 more.ts(2345)

I cannot see where this error is coming from. Thank you guys for any help!

4
  • Do not do this new BehaviorSubject(this.addressBook). You will trigger this expression has changed errors in Angular, and it is also an anti-pattern in functional programming. Commented Jul 8, 2019 at 8:22
  • what would be the better way to do this? the articles that I have read have the new BehaviorSubject(this.addressBook) Commented Jul 8, 2019 at 9:07
  • @Ctfrancia maybe you can leave out the whole BehaviorSubject part and directly access the addressBook. Commented Jul 8, 2019 at 11:27
  • Alright, I'll take a look at that @robert Commented Jul 8, 2019 at 11:59

1 Answer 1

1

Your BehaviourSubject should be instantiated as follows:

private readonly ab$: BehaviorSubject<Map<string, Contact[]>> = new BehaviorSubject(new Map({}));

Since it has to be same type as addressBook.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.