Skip to content
Advertisement

Tag: casting

Typescript Pick optionnal unset variable

I’m trying to use the “pick” function of typescript to get all the possible values of my objects. My objects have optional attributes so they are not necessarily set and I also try to convert “type” attribut to a type with : but I think there is a better way to do it and I don’t know where to use

Converting Element to HTMLElement in javascript / typescript

So, I’m getting a list of elements that match a selector using querySelectorAll, which stores them in a NodeList. I’m then scanning through the NodeList with a forEach loop, at which point the type of each individualItem is “Element”. However, I’m feeding these individualItems into a function “doThing()” that expects individualItem to be of type “HTMLElement” (I’m using typescript). I’m

Why does Javascript only type-cast for string concatenation on strings that are already assigned?

I noticed something strange when messing around with strings and numbers in a Javascript console. Doing this: “$99.9” += 0 causes SyntaxError: Invalid left-hand side in assignment. But if I assign the string to a variable, the behavior is different (and more in line with what we’ve come to expect/accept from Javascript) let str = “$99.9” str += 0 //

Casting a number to a string in TypeScript

Which is the the best way (if there is one) to cast from number to string in Typescript? In this case the compiler throws the error: Type ‘number’ is not assignable to type ‘string’ Because location.hash is a string. So which method is better? Answer “Casting” is different than conversion. In this case, window.location.hash will auto-convert a number to a

Advertisement