简体   繁体   中英

error TS2322: Type 'null' is not assignable to type 'UserAuth'

I create a UserAuth interface :

user-auth.module.ts

export interface UserAuth {
  id: string;
  name: string;
  email: string;
  created_at?: string;
  updated_at?: string;
}

, I declare an authUser object of this interface in chat.component.ts :

authUser = JSON.parse(localStorage.getItem('user') || '{}').value.authUser;

I receive this authUser object in list.component.ts :

@Input() authUser !: UserAuth;

I want to declare an object of this UserAuth interface list.component.ts :

changeToGroupChat() {
    const user : UserAuth = null; 
    this.selectedAuthUser.emit(user);
  }

but it gives me error:

error TS2322: Type 'null' is not assignable to type 'UserAuth'

Try to use union type of typescript:

const user : UserAuth | null = null; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM