title: Angular set null to User object on logout
body: <p>I am trying to build a simple user authentication app in Angular following this <a href="http://jasonwatmore.com/post/2018/10/29/angular-7-user-registration-and-login-example-tutorial" rel="nofollow noreferrer">tutorial</a>. I am facing an error when I am trying to set null to the User object after the user has logged out.</p>
  
  <p>Error:</p>
  
  <pre><code>ERROR in src/app/_services/authentication.service.ts(40,38): error TS2345: Argument of type 'null' is not assignable to parameter of type 'User'.
  </code></pre>
  
  <p>Relevant code (authentication.service.ts):</p>
  
  <pre><code>import {Injectable} from '@angular/core';
  import {HttpClient} from '@angular/common/http';
  import {BehaviorSubject, Observable} from 'rxjs';
  import {map} from 'rxjs/operators';
  
  import {User} from '@/_models';
  import { environment } from 'environments/environment';
  
  @Injectable({providedIn: 'root'})
  export class AuthenticationService{
      private currentUserSubject: BehaviorSubject&lt;User&gt;;
      public currentUser: Observable&lt;User&gt;;
  
      constructor(private http: HttpClient){
          this.currentUserSubject = new BehaviorSubject&lt;User&gt;(JSON.parse(localStorage.getItem('currentUser') || '{}'));
          this.currentUser = this.currentUserSubject.asObservable();
      }
  
      public get currentUserValue(): User{
          return this.currentUserSubject.value;
      }
  
      login(username: string, password: string){
          return this.http.post&lt;any&gt;(`${environment.apiUrl}/user/login/`, {username, password})
              .pipe(map(user =&gt; {
                  console.log(user);
                  // login successful if there is a jwt token in the response
                  if(user.status &amp;&amp; user.data.token){
                      // store user details and jwt token in local storage to keep user logged in 
                      // between page refreshes
                      localStorage.setItem('currentUser', JSON.stringify(user));
                      this.currentUserSubject.next(user);
                  }
              }));
      }
  
      logout(){
          // remove user from local storage to log user out
          localStorage.removeItem('currentUser');
          this.currentUserSubject.next(null);
      }
  }
  </code></pre>
  
  <p>I can solve the problem by setting <code>"strict": false</code> in tsconfig.json, but I want to follow the best practices. Any idea why this is happening and how to fix it?</p>
  
  <p><strong>Update:</strong>
  The User model is:</p>
  
  <pre><code>export class User {
      id!: number;
      username!: string;
      token?: string;
  }
  </code></pre>