News for hackers. Stuff that matters.
Re:TypeScript 4.1 by GiorgioG on Thursday November 19, 2020 @10:16PM (#25157021)
TypeScript only provides the illusion of type safety. At runtime, it’s just plain old JS where all bets are off. I’ve been bitten too many times. As long as there’s no native TS runtime were stuck pretending we have type safety.
Re:TypeScript 4.1 by saghm on Thursday November 19, 2020 @10:27PM (#25157064)
I'm not sure I understand this comment. I can believe that typescript might not be typesafe, but what does the runtime not having type information have to do with that? If a compiler only accepts sound programs, then it shouldn't matter whether the runtime actually knows about the types or not (unless you want to do some sort of reflection, but allowing that is arguably _less_ typesafe). By that logic, you'd also have to consider anything that compiles to native code also not to be typesafe, because there sure aren't types in assembly.
Re:TypeScript 4.1 by Townley on Friday November 20, 2020 @12:10AM (#25157480)
For me, the main place the issue above rears it’s head is in data retrieved from an API. A strongly-typed language would fail loudly and predictably if a blob of JSON didn’t match the expected format. In some situations, that’s preferable to having invalid data work it’s way through the system, or having to manually implement type checking

I’m a fan of the Runtypes library for this use case. If you design your typescript interfaces using the library (admittedly a bit magical to me) you can get runtime errors if API data doesn’t match the expected type

Re:TypeScript 4.1 by neurotrace on Friday November 20, 2020 @12:22AM (#25157541)
You can use guard functions to validate external data. This same problem comes up in other statically typed languages. You can't guarantee that external data will be in the shape that you expect so you must validate it. If you're just taking your `unknown` API response and immediately casting it to your desired type then that's your fault, not TS
Re:TypeScript 4.1 by eropple on Monday November 23, 2020 @01:09PM (#25189668)
That's what runtypes is.
Re:TypeScript 4.1 by Vinnl on Friday November 20, 2020 @04:55AM (#25158943)
Yeah, ideally JSON.parse() returned `unknown`, but unfortunately that was added after the current type definition was already in widespread use, and therefore would be too much of a breaking change. Perhaps some day with a compiler flag.
Re:TypeScript 4.1 by eyelidlessness on Saturday November 21, 2020 @01:52PM (#25171801)
There are a lot of core language types that could substantially improve safety (not to mention inference), with anything explicitly `any` being obvious candidates. I feel like this should be a compiler/lib option for those of us who would opt into the breaking change for increased safety.

But since it isn’t, augmentations may be an option. I’m not at my computer right now so I can’t verify that this would work, but you may be able to add an overload that returns `unknown`. Unfortunately with overloads, I’m pretty sure the built in types would be selected first without some additional specificity.

This feels like a good opportunity for a safer standard library package wrapping globals, DOM, Node built-ins, etc.

Re:TypeScript 4.1 by TheCoelacanth on Friday November 20, 2020 @12:56PM (#25162989)
Isn't that basically what the noImplicitAny flag does?
Re:TypeScript 4.1 by Vinnl on Friday November 20, 2020 @03:04PM (#25164342)
Unfortunately no: that only errors when you do not add a type annotation and TypeScript can't infer anything better than `any`. JSON.parse(), however, is explicitly annotated to return any. See [1].

[1] https://www.typescriptlang.org/play?#code/PTAEEkDsDMFMCd6wCa...

Re:TypeScript 4.1 by WorldMaker on Friday November 20, 2020 @06:26PM (#25166381)
Yes, the type signature for JSON.parse() isn't inferred/deduced, it's explicit in lib.es5.d.ts (imported from lib.d.ts):

https://github.com/microsoft/TypeScript/blob/master/lib/lib....

You could provide your own lib.d.ts with `unknown` type returned for JSON.parse. I'm almost surprised there isn't a standard stricter lib.d.ts, though I suppose that linters fill enough of those needs with their rulesets.

Looking at GitHub issues surrounding it, it doesn't appear that there has been an explicit ask to switch the type signature to use `unknown`, probably the next closest was this one, where `unknown` is mentioned in a comment as a possibility but the comment is not addressed in follow up discussion: https://github.com/microsoft/TypeScript/issues/26993

It would be a big breaking change though at this point, so they would probably be hesitant to do so. I thought I saw somewhere there was a proposal to add it as a compiler flag to treat all uses of `any` in lib.d.ts as `unknown`, but I have no idea what happened to that proposal and quick searches don't turn up anything.

Re:TypeScript 4.1 by Vinnl on Saturday November 21, 2020 @06:32AM (#25169109)
Yeah I'd swear I'd seen that too, but I can't find it quickly either.
Re:TypeScript 4.1 by TheCoelacanth on Friday November 20, 2020 @07:38PM (#25166848)
Ah, I see my mistake. I have been getting warnings about JSON.parse, but they are coming from eslint@typescript-eslint/no-unsafe-assignment. It partially serves this purpose, but probably not as comprehensively as actually having it return unknown would.
Re:TypeScript 4.1 by fy20 on Friday November 20, 2020 @04:10AM (#25158699)
You can use something like io-ts or zod to validate external data at runtime.
Re:TypeScript 4.1 by schwede on Friday November 20, 2020 @01:32AM (#25157820)
They are probably talking about accepting serialized data coming from a network request. Typescript doesn’t help at the edge if you just cast your JSON blob into your application model.
Re:TypeScript 4.1 by andyfleming on Friday November 20, 2020 @01:50AM (#25157890)
That's why you actually have to validate at the edge. TypeScript doesn't really change that fact. It only makes the decision of how you handle it explicit.
Re:TypeScript 4.1 by tomphoolery on Thursday November 19, 2020 @11:32PM (#25157328)
I feel like this is the same argument that people give when trying to justify not writing tests. Tools like type-checking and automated tests aren't a magic forcefield that will shield you from any and all runtime bugs. But they do protect against a good percentage of them, and as another commenter pointed out...70% type-checking is better than 0% type-checking.

The point of type-safety is to be able to describe more succinctly what the _programmer_ expects out of the code. As you said, when you get to production, "all bets are off". This still applies with any type-safe language. Types don't exist when you're compiling to machine code, either.

Re:TypeScript 4.1 by lmm on Thursday November 19, 2020 @11:49PM (#25157390)
> 70% type-checking is better than 0% type-checking.

I'm not sure it is; the most dangerous things in programming are assumptions that are almost correct, so you don't notice the problem until it's too late. An error whose symptom is an Int actually being a String several files away from where the mistake actually happened can be harder to diagnose than the same error in a untyped language, just because it's so contrary to your expectation.

Re:TypeScript 4.1 by yashap on Friday November 20, 2020 @02:47AM (#25158181)
Huh? The whole point of static typing, in any language, is basically:

- Catch more errors at compile time

- Better tooling (better intellisense, “go to definition”, “find implementations”, etc.)

TypeScript provides both of these benefits just fine. Runtime types give you better runtime errors, and less undefined behaviour, but plain JS (or TS compiled to JS) does that bit just fine. TS types persisting at runtime could maybe be nice for certain use cases, like more powerful reflection, but that’s not type safety.

What’s a concrete example where your program would be more safe if TypeScript types were represented at runtime?

Re:TypeScript 4.1 by eropple on Thursday November 19, 2020 @10:24PM (#25157049)
In my experience nobody is "pretending"; within epsilon of everyone knows this, and the experienced folks are writing code against it with something like `runtypes` or `io-ts` to sanitize on the edges of their application.
Re:TypeScript 4.1 by gitgud on Thursday November 19, 2020 @11:46PM (#25157382)
True, but it's the benefit of incremental adaptation of types on an old code based.

It also encourages better architecture in my opinion, as it forces you to reduce the importance of something being an exact type... As it's not strictly enforced at runtime.

Re:TypeScript 4.1 by valand on Thursday November 19, 2020 @10:25PM (#25157056)
Happened to me This helps me with it at the cost of a little bit more lines of code https://github.com/gcanti/io-ts
Re:TypeScript 4.1 by saiojd on Friday November 20, 2020 @07:51AM (#25159953)
I agree somewhat, the errors it misses are precisely the ones which are confusing.
Re:TypeScript 4.1 by b34r on Thursday November 19, 2020 @11:25PM (#25157300)
That’s what Deno is for
Re:TypeScript 4.1 by inbx0 on Thursday November 19, 2020 @11:41PM (#25157358)
Deno is also "just JavaScript" with types automatically stripped off. The selling point being "automatically". It doesn't do runtime type checking.
Re:TypeScript 4.1 by b34r on Friday November 20, 2020 @08:36AM (#25160283)
Ah you’re right. Weird, I could have sworn during the original announcement they were going to have support for runtime checking