Live data from Hacker News

How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

chiragswadia.medium.com

391–400 of 400 posts

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#391
post #266

Earlier quoted context omitted.

This comment and many others in this thread seem to be missing something. I see a lot of anger here or blame on the author for deliberately choosing to not use types, but I think the author actually does a very admirable job explaining what's going on in their head: "... concepts like Generics felt very hard to understand ... every piece of code is strongly typed and overwhelming. Even simple code like below scared m…

Not the parent, but I think it's possible to simultaneously believe two things: 1) It is completely understandable that, considering how easy it is to for a new programmer to get started in a dynamically-typed language (and that those languages are pushed very hard as beginner-friendly), many developers have a hard time grasping the idea of static typing and how to apply it to code, because their mental model just ne…

In the end, and in the big picture scheme of things, what is programming about? It's about providing value. There are plenty of systems and tech built that do not use types, yet they do provide value. And those systems may seem horrific to you, but none the less they provide value. These systems were built by hacking/mashing things together, but the fact that they work, still remains. You might hate for example WordPress, but it powers tons of web and those websites might not exist if it wasn't for WordPress.

Imagine a case where you can start to be productive with WordPress within few months, but if you were to learn the whole thing, it might take few years, the value provided would be a lot lower.

I went 4-5 years of my career without knowing anything about types, yet I believe I was able to provide software that benefitted the world.

It's not that black and white.

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#392
post #387

Earlier quoted context omitted.

I like io-ts, but I've moved mostly to runtypes: https://github.com/pelotom/runtypes I forget why Zod didn't pass muster for me now, but I've looked at it before and didn't find a compelling reason to jump to it. Thought it seemed good, though. I really wish at least one of these let you emit JSON schema back out, though. That'd be way easier for dealing with stuff like OpenAPI.

We go the other way. We start with openAPI, use dtsgenerator to spit out typescript types, and then use a library (typescript-is) that will generate run-time type check function as part of compilation. A downside to this is that you need to compile type changes before you can use the change in code, but it gives us a single source of truth for our type definitions and let's us generate a more exacting request and res…

Interesting! How do you wire it into validation, etc. for your web layer?

If you've got an example I'd love to see it. I've long been not a fan of OAS-first, but I'm always down to learn something new.

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#393
post #272

Earlier quoted context omitted.

> This comment and many others in this thread seem to be missing something. I think they're also shoehorning their experiences. Some coders are cogs in giant tech companies that emphasize code quality, others are on teams in large business in an unrelated field, others are solo hobbyists, and still others are the only tech guys at their medium or small business. Some write code to run a business, others write code to…

> I think they're also shoehorning their experiences. Yup, agreed. I first learned to program in the late 80s (when I was around 7 or 8 years old) using BASIC, and my first part-time programming job in college was a mix of C and Java. All of my EE and CS programming coursework was done in C, C++, or (pre-generics!) Java. It is really hard for me to put my feet in the shoes of someone who switched careers in their 20s…

I switched careers at 33 (currently 35) from structural engineering to a full stack JS career after attending a bootcamp.

Reading your comment, I wish I was in the former group of learning to code early on, taking CS and SE courses in university, etc... I work feverishly to learn as much as I possibly can about networking, Linux, hardware, SQL, etc.. along with all the JS related topics I need to learn for my job.

I just wanted to add to the conversation by saying that there are people in the later group who really want to become well rounded and not just be JS developers.

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#394

Earlier quoted context omitted.

Why did you hate react? It‘s really one of the less complicated frameworks out there.

It doesn't use native elements, and instead goes from React Components -> HTML Text -> Native Elements. So then the moment you have two elements interacting it falls on its face. Like forms.

Why do you think that? Forms can be easily manipulated with refs. A ref points to the native element. It‘s really not that hard

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#395
post #321

Earlier quoted context omitted.

I think everyone except JS devs wishes they could write something other than JS for the browser. Maybe eventually with WebAssembly...

Scala.js actually works really well. Frontend people aren't very aware of it, but if you're a backend dev who just needs to write a bit of code that has to run in the browser then I highly recommend it.

Even as an engineer happiest in Scala almost forced to write Typescript, I'll be the first to admit that the Scala.js library is still a bit lacking in comparison for real world productivity.

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#396
post #387

Earlier quoted context omitted.

We go the other way. We start with openAPI, use dtsgenerator to spit out typescript types, and then use a library (typescript-is) that will generate run-time type check function as part of compilation. A downside to this is that you need to compile type changes before you can use the change in code, but it gives us a single source of truth for our type definitions and let's us generate a more exacting request and res…

Interesting! How do you wire it into validation, etc. for your web layer? If you've got an example I'd love to see it. I've long been not a fan of OAS-first, but I'm always down to learn something new.

We like OpenAPI first because it's more exact than pure typescript types. I suppose there's libraries we could use to give us a similar level of exactness that would also get us typescript types and OpenAPI with similar exactness to openAPI first, but then that's another dependency that's not giving us anythign extra compared to openAPI first.

So the way we do it is a little meh because we have two separate run-time type validators, but it's because of the path we took where we adopted request and response validation almost from day 1, and other runtime validation came much later.

We're using express and express-openapi as the base of our project. Express-openapi has built-in request validation and type-coercion, and an example of how to quickly bolt on your own response validation, so for request and response validation we're using that. And it works. It's damn solid. But the docs for how to use the underlying tech to do similar run-time validation for say pulling values out of the db is lacking and I don't have enough expertise in the underlying tech to stand it up quickly, especially compared just dropping in typescript-is changing tsc to ttsc and adding an tsc plugin to our tsconfig.

If I were to do it again I'd either bite the bullet and spend the time figuring out how to get express-openapi's request validation infra to also do general type validation on just the schema, or i wouldn't use their run-time validation at all and just make my own middleware to do it, just so that I've only got a single run-time validation library. The nice thing about express-openapi is that since it "knows" the open-api definitions it can programmatically apply the types needed for validation, so rolling my own would require that functionality, because I don't want our team to have to remember to add the same middleware to every endpoint using a factory function that takes the typescript-is validator function as an argument, and the nice thing about typescript-is is that i can give it a typescript type and get a validator function exactly where i want it with no muss.

Anyway, our build looks like this: 1) compile typescript 2) compile the openAPI to a single file (we put the endpoint definitions in the endpoint file) using express-openapi (just initialize and then write out the JSON.stringify of the apiDoc field) 3) run dtsgen against the output of step 2, and pipe that through `sed 's/declare//g' | sed 's/namespace/export namespace/g'` and into some file.ts 4) compile the file from step 3 with typescript

step 1 gives us typescript compiled to js step 2 gives us a single openAPI.json file we can pass around or import into postman step 3 gives us all the types from openAPI in a ts file with a bunch of recursive exported namespaces step 4 gives us those types in a .d.ts file

We do 3 and 4 in a subdirectory so that we can publish the types as an internal package, which means we can give front-end a types package to consume for all the calls we want to make to backend. Well, we actually publish an sdk package that wraps the types and encapsulates the logic for making the HTTP request to the right endpoint, so we don't have to worry about calling POST when the endpoint is PUT or fat-fingering the response type cast through the entire front-end and can instead just call a function with well-defined types. Plus, now it's super easy to test the interface, because we have a single thing we need to test. So we write an exhaustive happy-path (ie 2xx status codes) integration test against the sdk that actually calls into backend gets the response and checks the response against the type and then the values we expect for each field.

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#397

Earlier quoted context omitted.

Why did you hate react? It‘s really one of the less complicated frameworks out there.

It doesn't use native elements, and instead goes from React Components -> HTML Text -> Native Elements. So then the moment you have two elements interacting it falls on its face. Like forms.

Really struggling to parse what you've said here

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#398
post #395
post #321

Earlier quoted context omitted.

Scala.js actually works really well. Frontend people aren't very aware of it, but if you're a backend dev who just needs to write a bit of code that has to run in the browser then I highly recommend it.

Even as an engineer happiest in Scala almost forced to write Typescript, I'll be the first to admit that the Scala.js library is still a bit lacking in comparison for real world productivity.

I had exactly the opposite experience. I kept trying to make Typescript work and it just wouldn't, eventually resorted to Scala.js and found (to my shock) that everything just worked and there were far less random rough edges.

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#399
post #398
post #395

Earlier quoted context omitted.

Even as an engineer happiest in Scala almost forced to write Typescript, I'll be the first to admit that the Scala.js library is still a bit lacking in comparison for real world productivity.

I had exactly the opposite experience. I kept trying to make Typescript work and it just wouldn't, eventually resorted to Scala.js and found (to my shock) that everything just worked and there were far less random rough edges.

That’s great :) Scala.js has such potential. I hope that my next opportunity to try it out (versus TypeScript) will be a similar experience to yours!

Re: How an Anti-TypeScript “JavaScript developer” like me became a TypeScript fan

#400

TypeScript is definitely an improvement over pure Javascript. However, you can still lie about the types and it still allows you to write nonsense (even in pure TS). > const x = 'xxxx' > if(x === undefined) { > console.log('never happens') > } will compile without any problems

TypeScript _does_ detect some such impossible conditions (like `x === 1`), so this omission is intentional. I think this is partly necessitated by one particularly large soundness hole in TypeScript, which is that expressions are typed as though dictionary and array subscripts are always in bounds: function foo(arr: number[]) { const m: number = arr[3]; // typechecks if (m === undefined) { // this IS reachable consol…

you're right about the dictionary lookup possibly being undefined (and the compiler flag there). Some people (including me) would really appreciate a `--hindley-milner-type-inference` compiler flag.
Post reply on HN