4

in my Angular app i have a component:

  filteredOptions: Observable<string[]>;

Following line give the error:

Property 'filteredOptions' has no initializer and is not definitely assigned in the constructor.

28   filteredOptions: Observable<string[]>;

1 Answers1

8

it's because typescript 2.7.2 included a strict class checking where all properties should be declared in constructor. So to work around that you can:

  • just add a bang sign (!) like: name!:string;
  • adding "strictPropertyInitialization": false to your compiler options.
  • use filteredOptions: Observable<string[]> | undefined;
  • use filteredOptions?: Observable<string[]>

more details:

  • name!:string; : compiler trust me on this, I will guarantee that it will not be undefined (can cause unpredicted behavior)
  • "strictPropertyInitialization": false: compiler stop your strict check everywhere please
  • the last two solutions: compiler, filteredOptions might or might not have a value. In that case, it's your responsibility to handle the undefined value in your case (use ?. in your HTML templates, null and undefined check...etc.)
Benzara Tahar
  • 2,058
  • 1
  • 17
  • 21