0

Here is my code. I am getting the error below.

type Filters = {
  type: 'expenses' | 'income'
  week: string
  month: string
  category: string
}
class SubjectManager {
  private subject$: BehaviorSubject<Filters> = new BehaviorSubject({
    category: 'all',
    week: '1',
    month: '10',
    type: 'expenses'
  })
}

Error

Type 'Filters' cannot be assigned to type '{ category: string; week: string; month: string; type: "expenses";

How can I fix this error?

2
  • the value "all" is assigned to the category property whose type is "string" Commented Nov 7, 2022 at 14:48
  • @BizzyBob Order of properties is not the same, so types are actually correct. Commented Nov 7, 2022 at 22:59

1 Answer 1

1

This works:

class SubjectManager {
  private subject$ = new BehaviorSubject<Filters>({
    category: 'all',
    week: '1',
    month: '10',
    type: 'expenses'
  })
}

The original code doesn't work because the object inside BehaviorSubject has type { type: 'expenses', ... }, not { type: 'expenses' | 'income', ... }.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.