15
       distanceX = ((this.frontclickedX  - originX) / originX).toFixed(2);
       distanceY = (-(this.frontclickedY - originY) / originY).toFixed(2);
       let distanceZ: number = (-(this.frontclickedY - originY) / originY).toFixed(2);

my browser throws an error when I calculated distanceZ:

Type 'string' is not assignable to type 'number'.

the first two lines work perfectly, but the third doesn't really work. As you can see it is even exact the same calculation as distanceY. I tried to parseFloat() it first. But then it starts to complain that I can only parse a string to a float, and that I didn't provide one.

I'm quite confused at the moment, so some help or a solution would be much appreciated!

Thanks in advance!

Edit: I tried to simplify my code. Made the distances private and added setters and getters.

private _distanceX: number;
private _distanceY: number;
private _distanceZ: number;

  get distanceZ(): number {
    return this._distanceZ;
}
set distanceZ(value: number) {
    this._distanceZ = value;
} ...
calculateBadgeDistance(): void{
        this.distanceX = (5.001).toFixed(2);
        this.distanceY = (5.001).toFixed(2);
        this.distanceZ = (5.001).toFixed(2);
}

Now I got the error on all 3 lines.

1
  • 1
    on which line does this occur? This snipped should work, if the variables are all numbers Commented Oct 10, 2016 at 11:22

3 Answers 3

30

The method toFixed() converts a number to a string. You have to parse the variables to numbers:

let distanceZ:number = parseFloat((-(this.frontclickedY - originY) / originY).toFixed(2));

Alternatively, temporarily store the string and parse it later

let distanceZTxt:string = (-(this.frontclickedY - originY) / originY).toFixed(2);
let distanceZ:number = parseFloat(distanceZTxt);
Sign up to request clarification or add additional context in comments.

8 Comments

This worked for me now. But do you know why it worked for dinstanceX, distanceY but not for distanceZ? Thanks a lot for the fix :)
Maybe you didn't define any type for distanceY and distanceX in the first place? If not, the variables will accept any data type.
I did, they are all private variables with setters and getters private _distanceX: number; private _distanceY: number; private _distanceZ: number;
If they are all defined with the data type number, the error should occur on all variables unless you parse them from string to number. I see that with your updated code, you defined them as numbers, but are you sure they were numbers in your first attempt? Otherwise I am not sure.
Yes, I defined them in the function like this: let distanceX, distanceY, distanceZ: number;
|
0

you can write your function like that to avoid this issue

get distanceZ(): number | string {
    return this._distanceZ;
 }

add "| string " with your function

Comments

0

Try this

((this.frontclickedX  - originX) / originX).toFixed(2) as any

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.