2

When i make property as id!:string; or id:string=''; when i assign the value of params i get an error

(property) MoreParametersComponent.id: string ts(2322)Type 'string | null' is not assignable to type 'string'. Type 'null' is not assignable to type 'string'.

import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-more-parameters',
  templateUrl: './more-parameters.component.html',
  styleUrls: ['./more-parameters.component.css']
})
export class MoreParametersComponent implements OnInit {
  id!: string; // or id:string='';
  id2:number=0;
  constructor(private activatedRoute:ActivatedRoute) { }

  ngOnInit(): void {
    this.activatedRoute.paramMap.subscribe(params=>{
      this.id=params.get('id'); // gives the above error here
      this.id2=parseInt(params.get('id2'));//error
    });
  }

}

How do I get around this error?

My Rest of the Code:

app.component.html

<a routerLink=""> Home </a><br>
<a routerLink="one-parameter,'p01'"> One-Parameter </a><br>
<a routerLink="more-parameter,{id:'p02',id2:123}"> More-Parameter </a> 
<br><br>
<router-outlet></router-outlet>
<br><br>

Route.config.ts

import { Routes } from '@angular/router'
import { HomeComponent } from './home/home.component';
import { MoreParametersComponent } from './more-parameters/more-parameters.component';
import { OneParamterComponent } from './one-paramter/one-paramter.component';


export const mainroute:Routes=[
    {path:'',component:HomeComponent},
    {path:'one-parameter',component:OneParamterComponent},
    {path:'more-parameter',component:MoreParametersComponent}
];
6
  • What's the problem exactly? It looks like params.get may return null, and null is not assignable to this.id. You need to handle this case. Commented Dec 18, 2021 at 9:23
  • Now i perform some changes on my code by adding some necessary code about this problem. Commented Dec 18, 2021 at 9:39
  • you can always use this.id=params.get('id') || null or this.id2=parseInt(params.get('id') || 0) Commented Dec 18, 2021 at 10:20
  • Just set "strictPropertyInitialization": false in tsconfig.json under compilerOptions and assign the value. I think dealing with this is just unnecessary overhead. Commented Dec 18, 2021 at 10:30
  • id: string | null; Commented Dec 18, 2021 at 10:38

2 Answers 2

3

The return of ParamMap.get is string | null which does not match your id type.

You could cast the return of ParamMap.get to a string like params.get('id') as string

Or you could subscribe on params instead which returns an object with property type any and access the the id param as an object key params.id. See https://angular.io/api/router/Params

Sign up to request clarification or add additional context in comments.

3 Comments

thanks!! Mehyar for Responding.
"Casting" it via as string will only tell the compiler to treat the value as string. In fact, it still may very well be a null value and cause errors along the way. A better solution would be to add an additional check whether the value is null or not and only use the return value if it's a valid useable value.
Which kind of errors will be caused along the way? When a null is passed as a param, this means that the problem happened before when the route passes a wrong value for the ID param. This should be prevented before receiving the ID value from the param. But of course it is not a bad idea to check on the ID value in the activated Route, but it's also not definitely needed
0

You can try adding an exclamation '!' at the end to let the compiler know that the id will not be null.

this.id=params.get('id')!;
this.id2=parseInt(params.get('id2')!);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.