Working on an Angular 11 project using Typescript with Strict Mode, I encountered the following issue:
export class AvatarComponent {
@Input() user: UserModel = null;
}
This resulted in a compilation error:
Type 'null' is not assignable to type 'UserModel'.
The UserModel
class is defined as:
export class UserModel {
id?: number;
name?: string;
}
How can I properly set the variable type of user
to UserModel
while allowing it to be null?