1
 //I am working on this assignment objToSendServer.input1 = obj.input1 //type definitions are: class obj { input1: number; } class objToSendServer{ input1: number } // if I do this assignment that value of obj.input1 is "1" // which cause of problem on server side // which is web api odata, patch method that expects type of int objToSendServer.input1 = obj.input1; // if I try cast as below, error message is: //Argument of type 'number' is not assingable of parameter of type 'string' objToSendServer.input1 = parseFloat(obj.input1); //My work around is: objToSendServer.input1 = parseFloat(obj.input1.toString()); 

I suppose that type cast would be done automatically which is not case here.

So my question is there better approach to this type of assignment

Also if obj.input1 is null this workaround fails.

TypeError: Cannot read property 'toString' of null 

Then I should check if obj.input1 is null or not

Note: I edited the title which was Is there "automatic" type cast in typescript to express issue more clear

4 Answers 4

4

The following code handles non-numbers, empty strings, undefined variables, null variables, and even numbers. It supplies a default value of 0 for all the "error" cases.

objToSendServer.input1 = +obj.input1 || 0; 

Here is a simplified demo. It shows four possible inputs (that are either a string or a number) and then uses slacker parsing to get a value.

var a: string | number = "1"; var b: string | number = 2; var c: string | number; var d: string | number = "A"; var w: number = +a || 0; alert(w); // 1 var x: number = +b || 0; alert(x); // 2 var y: number = +c || 0; alert(y); // 0 // It even infers the `number` type: var z = +d || 0; alert(z); // 0 

If you want to preserve null values, you would have to add a special case for that.

var n = (a === null) ? a : +a || 0; 

Remember that null is distinct from undefined, so you will get 0 for undefined.

If you want only the parsed number, or null (i.e. null is the default for any value that fails the parse):

var n = +a || null; 
Sign up to request clarification or add additional context in comments.

4 Comments

I am telling about in my question I should check if obj.input1 is null or not. Is there a way better than ` if obj.input1 is null ` according to your answer.
The example I have supplied checks for nulls already. The expression variable || 0 will return 0 if variable is null.
But the if input is null the result remain null as well not 0
I have added some additional options you can choose from depending on what you are after.
2

This is probably not the answer you're looking for, but you should solve this issue at the source by ensuring that the value in obj.input1 is always a number in the first place.

obj.input1 = (typeof val === "string") ? parseFloat(val) : val; 

Note: val being, whatever you're assigning to obj.input1.

3 Comments

@asdf_enel_hak doing typeof val === "string" will typeguard the type of val to string so .toString() would be unnecessary.
Could you complete ... then I could try in my programme your solution
@asdf_enel_hak i don't know how obj.input1 is being assigned to in your code. My main point is that when you assign to obj.input1, you should ensure the value being assigned is a string and not a number.
0

I think you're coming from a wrong assumption and trying to make the language work from there.

There is no "casting" to be done automatically since parseFloat() accepts strings only and you have to be more clear about how you want to convert values. You have a property of type number, which in your code example may have a string, in which case you want to do a different thing. So it's more than casting.

Setting a string value to a number would have errors of its own, but let's ignore that for now.

First, if you may have different types on the same property, you should use union types:

class obj { input1: number|string; } 

And then your code should interpret it by checking the type, like David Sherret answered:

objToSendServer.input1 = typeof obj.input1 === 'string' ? parseFloat(obj.input1) : obj.input1; 

TypeScript will correctly interpret the type check conditional and provide you with type checking for each case.

1 Comment

String is coming from ng-model of angularjs from interface. When value assigned it is string instead of number. so input1: number|string; is a desirable definition.I just should handle type cast
0

Is there “automatic” type cast in typescript

No. There is type compatiblity and structural typing.

// if I try cast as below, error message is: //Argument of type 'number' is not assingable of parameter of type 'string'

The issue is that parseFloat accepts only a string and you are passing it a number. If obj.input1 is a number You do not need to do a parseFloat on it.

1 Comment

String is coming from ng-model of angularjs from interface. When value assigned it is string instead of number

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.