馃懡 captainrantcraft

So ... what exactly does TypeScript do that's so great? I'm playing with it a bit and can see some benefits like better auto complete, etc. But it doesn't seem to provide any guarantiees. In my limited understanding it's just as flimsy as vanillia JS with the added downside of having to fuff around with various type declarations. A breaking example for me would be an API result. I get a JSON object that's likely going to look like the type. But how could I even make sure? Typescript will just as happily fail in this case as JS. Am I getting this all wrong?

2 months ago

Actions

馃憢 Join Station

5 Replies

馃懡 captainrantcraft

OK, zod is cool. Found some other libs but zod took about 3 minutes to get into. Adapted my little test project to use it all the way through. Does help to avoid a lot of sanity checking against whatever the user puts in or the database spits out. Defining a schema like that reminds me a little bit about Django models. I wonder if I could get this to work all the way spitting out the apropriate SQL, SurQL or whatever files reflecting the zod schema. 路 2 months ago

馃懡 captainrantcraft

Wrapped my head around it a little more. The zod library seems to be the key here. Declare a schema, infer the type from it and parse any result to see if the external results are correct. Not so different from vanilla JS as there I'd also check the result in some way. zod makes it quite streamlined. 路 2 months ago

馃懡 sdfgeoff2

Re: api types, yeah it doesn't validate. But if you are autogenerating your typescript definitions from your API (eg from an openapi schema document), then you know they match. I suggest you autogenerate your whole client if you can.

In a large webapp, the API routes are a small part but can propagate quite deeply, so the frontend is sensitive to API changes. Typescript can then guard the entire app: you create a Merge Request/changes for an API route, regenerate the frontend typescript bindings and make sure there are no reported issues. Then you know that a rollout of the API won't cause staging issues upon deployment. 路 2 months ago

馃懡 sdfgeoff2

The bigger the project the more useful it is.

Principally it moves issues from manifesting as errors on production systems by making them available as warnings at development/build time. This makes it easier to ship more stable software. It is extremely useful when built into CI/CD

Ever forgot to check for a none/null? Or added a parameter to a function and had to go around and hunt down all the invocations. Or accidentally passed a date as a string rather than as a date object etc. etc. Typescript will help you find these issues. 路 2 months ago

馃懡 devyl

Since it is just a "wrapper" around vanilla js it is there to make the life of a developer easier. i stay with vanilla js to see whats really going on in my code. 路 2 months ago