Re:The type system is a programmer's best friend
by noduerme on Saturday November 05, 2022 @01:44PM
(#33483719)
Casting is often necessary for parsing inbound data from certain mysql libraries or CSV or JSON depending on how it's written. I would guess that might be what the parent is talking about. That said, if you don't cast or parseFloat or whatever in JS you're going to have a lot of trouble. And if you're doing that, why not do it in Typescript where you'll know that the data you're accessing has been safely cast based on its type.
[ Parent
| on HN ]
Re:The type system is a programmer's best friend
by xigoi on Saturday November 05, 2022 @04:50PM
(#33485549)
> Casting is often necessary for parsing inbound data from certain mysql libraries or CSV or JSON depending on how it's written.
No, that's what sum types are for.
Re:The type system is a programmer's best friend
by noduerme on Monday November 07, 2022 @04:14PM
(#33512553)
I don't see how they're mutually exclusive. I use union types prior to typeguards to end up with ultimately checked, cast values. Say I have a boolean fetched from JSON as "1" or "0". By the time I expose it to the rest of the code as part of a Record, I want to change it to an actual boolean. At first I'm going to treat the inbound value as a union type, e.g. (String | Number | null | undefined | Error). After I deal with the error cases, I'm going to cast it (or in TS, reinitialize it as a single type, boolean) so that any code looking at the imported value sees it as definitely only a boolean without needing to have lots of different pieces of code run their own checks on its type.