Within my Angular 12 application, I am working with the following UserModel class:
export class UserModel {
id?: number;
email: string;
}
In a specific component, I am trying to retrieve both the id and email from the route parameters using the following code snippet:
ngOnInit() {
this.route.paramMap.subscribe(
(value: ParamMap) => {
this.user = {
id: value.has('id') ? value?.get('id') : undefined,
email: value.has('email') ? value?.get('email') : undefined
}
});
}
However, I encounter an error specifically related to the id
:
Type 'string | null | undefined' is not assignable to type 'number | undefined'.
Type 'null' is not assignable to type 'number | undefined'.
I attempted another approach as well:
id: value.has('userId') ? +(value?.get('userId')) : undefined,
which resulted in the error:
Object is possibly 'null'
Could anyone provide guidance on how to resolve this issue?