-1

I have confusion in the second line. This code is working but I need to understand what it actually means.

export class EmployeeService {
  employees!: EmployeeClass[];  // I think it should be *employees: EmployeeClass[] = [];*
  constructor(private http: HttpClient) {}

  getAll(): Observable < EmployeeClass[] > {
    return this.http.get('http://localhost/website/employees_ng').pipe(
      map((res: any) => {
        this.employees = res['data'];
        return this.employees;
      }),
      catchError(this.handleError));
  }
Billu
  • 2,733
  • 26
  • 47
  • Related: https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci – Jeppe May 20 '21 at 05:36
  • 1
    Good question, but it's been asked/answered before: [In Typescript, what is the ! (exclamation mark / bang) operator when dereferencing a member?](https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci). And no, I didn't mark you down - and I don't know *why* anybody marked you down :( – paulsm4 May 20 '21 at 05:46
  • @paulsm4 please see now. Question updated, please upvote my question – Billu May 20 '21 at 12:52

2 Answers2

3

That's TypeScript's Definite Assignment Assertions(!).

From the TypeScript Docs:

Definite Assignment Assertions

The definite assignment assertion is a feature that allows a ! to be placed after instance property and variable declarations to relay to TypeScript that a variable is indeed assigned for all intents and purposes, even if TypeScript’s analyses cannot detect so.

So it basically is a means to tell TypeScript to stop complaining about employees being undefined right up front as they would be certainly initialized in your code, eventually.

SiddAjmera
  • 38,129
  • 5
  • 72
  • 110
  • it is okay to write "employees: EmployeeClass[];" instead of "employees!: EmployeeClass[];". If yes, then why angular complaining this error: Property 'employees' has no initializer and is not definitely assigned in the constructor.ts(2564) – Billu May 20 '21 at 05:49
  • Please, read my post. It'll answer this question. –  May 20 '21 at 05:51
  • @MuhammadBilal Reading [this article](https://www.bennadel.com/blog/3539-using-the-definite-assignment-assertion-to-define-required-input-bindings-in-angular-7-1-1.htm) and this [GitHub thread](https://github.com/angular/angular/issues/24571) might help. – SiddAjmera May 20 '21 at 05:54
2

It depends on what your question is pointing at.

employees!: EmployeeClass[];

The exclamation mark tells the compiler that the variable will not be null or undefined at runtime. So there is no need to complain about it from compiler's side.

The line of code in a whole says that there is a variable employees of type Array of EmployeeClass which is currently not instantiated but will be at runtime.

You could do this

employees: EmployeeClass[] = [];

This way you always start with an empty array of type Array of EmployeeClass and you do not necessarily need the exclamation mark any more because it is instantiated and the compiler does not report a missing initializer.