1

I had a bit of confusion today when my app was behaving strangely. Turns out I was assigning a string to a number, but there was no error alerting me of that. Any idea why that might be?

id:number;

later:

this.id = ActiveRoute.params.id; //(this was a string, obviously)
//do some iteration (n=this.id) times

My iteration was only running once, but I would have expected to be alerted to the type misalignment when assigning to id.

(edited attribute originally from memory^)

4
  • 1
    what is ActiveRoute? and what is props? angular-router uses ActivatedRoute.params Commented Aug 26, 2020 at 2:08
  • Going from memory since my work compy wasn't open, and those aren't the important parts. the important part is I defined a number in TS, and it let me assign a string to it without warning me. Commented Aug 26, 2020 at 3:28
  • Because it is typed as any not string. Commented Aug 26, 2020 at 12:45
  • @AluanHaddad id:number; is typed as any? Commented Aug 26, 2020 at 22:38

1 Answer 1

2

I'm assuming ActiveRoute is of type ActivatedRouteSnapshot, of which, the type of property params is Params, which meets the interface { [key: string]: any }

that any part there is the important bit, TS doesn't know the type of the properties in the Params object, so it takes your word for it when you assign it to a property with type number. any really means TS will let you assign it to any type. You need to make sure you assign correctly.

docs: https://angular.io/api/router/ActivatedRouteSnapshot#params

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

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.