Cast object to interface in TypeScript

There’s no casting in javascript, so you cannot throw if “casting fails”.Typescript supports casting but that’s only for compilation time, and you can do it like this: You can check at runtime if the value is valid and if not throw an error, i.e.: Edit As @huyz pointed out, there’s no need for the type assertion because isToDoDto is … Read more

Java rules for casting

When can a certain object be cast into another object? Does the casted object have to be a subtype of the other object? I’m trying to figure out the rules… Edit: I realized that I didn’t explain my issue at all: basically I am casting an object to an interface type. However, at run-time, I … Read more

Why cannot cast Integer to String in java?

Why this is not possible: Because String and Integer are not in the same Object hierarchy. The casting which you are trying, works only if they are in the same hierarchy, e.g. In this case, (A) objB or (Object) objB or (Object) objA will work. Hence as others have mentioned already, to convert an integer to string use: String.valueOf(integer), or Integer.toString(integer) for primitive, … Read more

CAST to DECIMAL in MySQL

From MySQL docs: Fixed-Point Types (Exact Value) – DECIMAL, NUMERIC: In standard SQL, the syntax DECIMAL(M) is equivalent to DECIMAL(M,0) So, you are converting to a number with 2 integer digits and 0 decimal digits. Try this instead:

How to cast ArrayList<> from List<>

When you do the second one, you’re making a new arraylist, you’re not trying to pretend the other list is an arraylist. I mean, what if the original list is implemented as a linkedlist, or some custom list? You won’t know. The second approach is preferred if you really need to make an arraylist from … Read more

Typescript: Index signature is missing in type

The problem is that when the type is inferred, then the type of o is: That’s not the same as { dic: { [name: string]: number } }. Critically, with the top signature you’re not allowed to do something like o.dic[‘x’] = 1. With the 2nd signature you are. They are equivalent types at runtime (indeed, they’re the exact … Read more