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

Behader

I really did not understand how the type assignment system works for variables in typescript. Can anyone help me why this does not work. Thank you

valorParcela:number;
totalCost:number;
VendaProdutoVendaNParcelas: number;

this.valorParcela = Number( this.totalCost / this.VendaProdutoVendaNParcelas).toFixed(2);

I expected a simple parseFloat

Titian Cernicova-Dragomir

toFixed returns a string which you are assigning to a variable which should be number. You need another pair of () to convert the result of toFixed back to number

this.valorParcela = Number(( this.totalCost / this.VendaProdutoVendaNParcelas).toFixed(2));

Or you can use the unary + trick to perform number conversion:

this.valorParcela = +(this.totalCost / this.VendaProdutoVendaNParcelas).toFixed(2);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Type number is not assignable to type string

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

Type 'number' is not assignable to type '[string | number, string]'

Type 'string | number | symbol' is not assignable to type 'string'

argument of type 'number' is not assignable to a parameter of type 'string'

Type 'string | number' is not assignable to type 'never'

Typescript Type 'number' is not assignable │ to type 'string'

Argument of type 'number' is not assignable to parameter of type 'string'

Error: Type 'string' is not assignable to type 'number' with toFixed()

TypeScript error: Type 'string' is not assignable to type 'number | ""'

Type 'number' is not assignable to type 'string'. ts(2322)

Argument of type 'string | number' is not assignable to parameter of type 'string'. Type 'number' is not assignable to type 'string'

Argument of type 'string[]' is not assignable to parameter of type 'number[]'. Type 'string' is not assignable to type 'number'

Argument type number is not assignable to parameter type string | undefined Type number is not assignable to type string

Type 'string' is not assignable to type '"" | "," | " " | "."'

Type '{}' is not assignable to type 'string'

Type string is not assignable to type

Typescript compiler never Error: Type 'string | number' is not assignable to type 'never'. Type 'string' is not assignable to type 'never'

Type 'string | number | boolean' is not assignable to type 'undefined'. Type 'string' is not assignable to type 'undefined'.ts(2322)

Type number[][] is not assignable to type number[]

Angular Type 'string' is not assignable to type 'Number' ...but it never is a string

Argument of type 'string | number' is not assignable to parameter of type 'string'

Argument type 'string | null' not assignable to parameter type 'string | number | boolean'

Type '{ |cssProperties|}' is not assignable to type 'Properties<string | number, string & {}>' in React

string[] is not assignable to type string

Type 'number' is not assignable to type 'ReadonlyArray<{}>'

Type 'number' is not assignable to type 'TeardownLogic'

Type 'null' is not assignable to type 'number'

Type 'number' is not assignable to type 'never'