News for hackers. Stuff that matters.
Re:From TypeScript to ReScript by pcthrowaway on Wednesday January 12, 2022 @10:23AM (#29907576)
Is it even possible for the program to know at compile-time if the types will be violated at run-time? Even if you declare data from other sources 'not mine' this still seems like it would be impossible to do.
Re:From TypeScript to ReScript by jayshua on Wednesday January 12, 2022 @11:32AM (#29908915)
The type system can force you to handle both cases - the data being in the expected & typed form, and the data not being in the expected form.

You would have a function like this:

parseJsonString : String -> Result Error Person

Where Result is a value that is either an Error or a Person.

To get the Person value out, the type system forces you to also do something with the Error if it exists. Not handling both possibilities in some way is a type error.

Re:From TypeScript to ReScript by ragnese on Wednesday January 12, 2022 @01:13PM (#29910677)
> Is it even possible for the program to know at compile-time if the types will be violated at run-time?

I mean, that's kind of the whole point of static typing, right?

If you're speaking to TypeScript/JavaScript, specifically, then yeah, the compiler can't guarantee anything because someone can hop into the running program and start modifying objects as they please (at least for web page code).

But the compiler could still make the weaker guarantee that "the code that I can see is soundly typed". And for 99% of us that's effectively the same thing.

Re:From TypeScript to ReScript by danenania on Wednesday January 12, 2022 @11:30AM (#29908872)
You can accomplish it with a library like zod[1]. It's not the prettiest though; first class support would definitely be better.

1 - https://github.com/colinhacks/zod

Re:From TypeScript to ReScript by bern4444 on Wednesday January 12, 2022 @12:48PM (#29910285)
As long as the data is from within the system, yes. If you're trying to use data from an external source (api request, external json file etc) than no, but that's true of most type systems.

If you correctly type the response of that request then the answer becomes yes again.

Re:From TypeScript to ReScript by pcthrowaway on Wednesday January 12, 2022 @01:40PM (#29911083)
Maybe I misunderstood, but I thought the person I was responding to was suggesting having the option for sound typing enforced in typescript, at compile time. Because if it's at run-time... well the run-time is Javascript, so there's of course no typing there. This isn't really on Typescript

There are however javascript libraries which allow you specify object schemas and throw errors when mismatches occur. Perhaps the suggestion then is to build some shared layer between the TS compiler and V8 so that similar schemas can be autogenerated by the TS compiler and enforced at run-time.

Re:From TypeScript to ReScript by rsrsrs86 on Wednesday January 12, 2022 @07:29PM (#29915055)
There is generally no typing in run time. One of the practices in writing compilers is to skip type checks during run time, because you know beforehand that no type errors are possible. That is type erasure.

If you're interested, lookup existential types. Very interesting stuff.

Re:From TypeScript to ReScript by nesarkvechnep on Wednesday January 12, 2022 @01:29PM (#29910915)
Take a look at Elm.