News for hackers. Stuff that matters.
Re:The silent majority in software by danielvaughn on Monday August 29, 2022 @08:36AM (#32637402)
As someone who really appreciates strong typing, I will die on this hill - I hate Typescript.

Introducing a transpiled typing system on top of a dynamically typed language is a recipe for all sorts of insane complexity, which is what I experience whenever I try to use TS. If you consider all the time and effort it took to introduce types for every NPM project out there, all the development of the language itself, the effort expended by individual devs and teams to learn the syntax...

...might it have been the same amount of effort just to introduce some strong types into Javascript itself? It's not like the language can't adapt, and there are already some strong types when you're working with the graphics APIs.

Re:The silent majority in software by jongjong on Monday August 29, 2022 @02:32PM (#32641749)
I'm in a similar boat. I appreciate static typing and although I don't have any strong objections to TypeScript purely as a language, I can't get past the way it's transpiled into a dynamically typed language with source maps... Seems like a hack.

It doesn't make sense; kind of like transpiling lower level assembly code into higher level C code. You could always argue that there is value in forcing arbitrary constraints onto developers but in the end, you need to ask the question "is it worth the complexity?"

Re:The silent majority in software by overgard on Monday August 29, 2022 @10:07AM (#32638432)
To me, TypeScripts "complexity" is just from documenting the weird things JS libraries do. If it encourages less weirdness Im happy
Re:The silent majority in software by jollybean on Monday August 29, 2022 @10:05AM (#32638414)
I consider JS an anti-pattern and won't allow more then 50 lines to be anywhere near me without TypeScript. The issues raised are innumerable.

TS is really flexible and you can do some crazy gymnastics with it - I don't favour that. Just keep it very simple and don't go crazy with the generics.

I've never had a problem at all, I've never had to dig into what was transpiled.

Re:The silent majority in software by danielvaughn on Wednesday August 31, 2022 @08:12PM (#32670047)
Someone else in the thread mentioned how The Good Parts™ idea applies to TS just as much as JS, and I think that's what kinda frustrates me about it. If they'd just kept the damn thing simple, I don't think I'd have a problem with it.
Re:The silent majority in software by wnolens on Monday August 29, 2022 @09:50AM (#32638218)
Did a personal web project and chose TypeScript to see what the rage was about.

I spent a ridiculous amount of time trying to design types. At some points I couldn't figure it out, or I ran into some limitation of the language to express what I need. So my code has a bunch of exclamation points everywhere to assert that a null ain't coming. It feels dirty.

Just too hard to use for me.

Re:The silent majority in software by llllIIIIllll on Monday August 29, 2022 @11:00PM (#32646178)
After years and years in C# I spent half a year writing Typescript full time.

IMO it's not that Typescript is limited at all, it's just that the type system is different, more complex and also more flexible - there are typically more ways to define types compared to Java/C#, so a lot more decision points trying to write something elegant, or even just idiomatic/understandable, so IMO the learning curve is steep. And then you get to the fun stuff with more abstract type definitions, infer keyword...

It screwed with my head way more than I thought it would. Structural typing was really different, I had to spend a long time rewiring my brain. But when I went back to C#, I felt like the type system was crude. I lost all the nice and easy ways of writing dynamic code and had to revert to defining surplus kludgy interfaces, delegates, etc. And in Typescript I'd finally figured out how to write null-safe code and remove almost all ugly coercions, then back in C# null references were a real possibility.

Re:The silent majority in software by wnolens on Tuesday August 30, 2022 @12:16AM (#32646582)
Interesting, yeah I had one or two good moments after spending a few hours wrangling my interfaces.

But the frustration left a worse impression than did the benefits. Perhaps if I was being paid to do it and had mentors around to ask for help. But I couldn't find any good advice online.

Re:The silent majority in software by llllIIIIllll on Tuesday August 30, 2022 @11:57PM (#32658215)
I had a pretty terrible experience at that job - no engineering manager, never had any code reviewed, hardly any collaboration with other team members so I was basically stabbing away in the dark trying to get my shit together in TS.

It took me far longer than a few hours to feel comfortable, probably more like over a month of coding to get to the point where I mostly understood how to express what I wanted with the type system, and had stopped doing C# things in TS and adhering to style guidelines - functional modules, types instead of classes, injecting dependencies etc. And another month or two? to really figure out how everyone has been (ab)using the type system to do more complex stuff.

Re:The silent majority in software by overgard on Monday August 29, 2022 @01:10PM (#32640709)
A few things you might consider...

Are you using "?" optional fields in your classes/interfaces a lot? If so, do you need to? Unless it's truly optional, you're basically making the type system less useful if you put that all over the place. If you have a lot of sparse objects, it's probably an indicator of a data type doing too much. You might be much better off splitting it into smaller interfaces and using union types when you need combined objects.

Even if you can't do the above, checking for null at the start of a function/block will "prove" to the compiler that it's not-null for the rest of the block, so an upfront check can save you from a lot of "!" assertions.

Even if you can't do either of the above, you can probably use the elvis operator "?." to safely dereference things that might possibly be undefined.

In my experience with typescript, there's a few good rules of thumb and things to keep in mind:

- Prefer "interface" over "class" for describing most data types. Especially plain old data objects that don't have methods. I'd forget about the definition of "interface" you might know from Java. It can be used that way, but it can also be used like you would use "struct" in C

- Use the "Partial" modifier instead of using "?" in the base structure if you need to do something like sparse updates. For instance a pattern I use a lot is:

   interface MyStruct { a: number, b: string }
   
   function update(original: MyStruct, updatedFields: Partial<MyStruct>) {
      return {...original, ...updatedFields}
   }
- Always prefer readonly fields and non-optionality.

- If you're not worried about GC pressure, it's almost always better to return a new object than mutate an old one. The spread operator is great for this.

- Within reason, it's better to prefer smaller data structures and use type unions when you need fields from both in one object.

Re:The silent majority in software by wnolens on Monday August 29, 2022 @05:15PM (#32643642)
Thanks. It's definitely not obvious to me coming from Java/c++/python.

I think I went into the deep end with some non trivial generic functions. It's been a few months, but I remember ditching interfaces for classes so I could do some run time type checking.

I wouldn't have fallen into these traps with something like Java.

Re:The silent majority in software by rektide on Monday August 29, 2022 @11:01AM (#32639129)
It's just so awesome that a thread on "Silent Majority" has the typical loud protests against JavaScript, against the web, against React, against TypeScript. The irony is loud to me.

It's not even that there's only happy heads-down programmers off HN who support these mainstream techs. Even in the active userbase here, I'd wager there are tons of people who have an adequate or better experience with typescript or react, and may disagree with the dislikers. But we're not as invested in our liking, don't feel like it's worth engaging in, and none of the banter changes what is or where things are going. These are the de-jure tools that the majority definitely uses widely, which at least the first anti- comment, on front-end frameworks, acknowledged. Whatever the targets-of-the-day happen to be, they will crop up repeatedly, which further dissuades from trying to come back with a positive or even neutral opinion that might represent that silent majority's take. And the silent majority must constantly face Brandolini's Law; what it's doing may be working & have good reason, but it's hard to keep supporting & reasoning through with a pact invested in disbelieving.

I liked the top post talking about disagreeing with the direction. That can be contributive, be value based. So often though the protests amount to gatekeeping "you've done too much." I evaluate where these comments would fall on Steve Yeggie's Software Political axis, from Notes from the Mystery Magic Bus[1], and almost universally the voices are conservative; they object rather than suggest, they refuse rather than support. I'd like to see more progressive protests, that can surface good, that acknowledge value & need but reflow or redirect the energy elsewhere, rather than being unhappy with what is.

[1] https://gist.github.com/cornchz/3313150

Re:The silent majority in software by wnolens on Monday August 29, 2022 @05:54PM (#32644047)
HN can be conversational, not always a well researched debate. Most of us are over-involved with these debates with coworkers at our day jobs, where we HAVE to spend the energy having productive disagreements.

I appreciate the high quality HN posts, but sometimes just like at a water cooler a bunch of us just wanna say, "agh I'm so sick of object oriented" and it doesn't mean "all object oriented programming is of no use".

Re:The silent majority in software by vbezhenar on Monday August 29, 2022 @01:05PM (#32640640)
What's so hard about designing types? I'm Java developer and when I need to use TS, I just write Java with it. Works fine. The only times I encounter weird types is when for some reason I need to read type definitions for some weird JS library. But that's relatively rare.
Re:The silent majority in software by danielvaughn on Monday August 29, 2022 @07:56PM (#32645050)
I’ve found that working with the DOM can be really difficult because the types built into TS don’t accurately capture the underlying API. In just a few hours I found 2 cases where it seemed obvious that the type definition was incorrect.
Re:The silent majority in software by wnolens on Monday August 29, 2022 @05:55PM (#32644056)
Good for you.
Re:The silent majority in software by mikewhy on Monday August 29, 2022 @11:31AM (#32639461)
Threads like these are why I don't pay attention to HN anymore.
Re:The silent majority in software by wnolens on Monday August 29, 2022 @05:55PM (#32644066)
I'm sorry I didn't sufficiently entertain you.
Re:The silent majority in software by striking on Monday August 29, 2022 @09:24AM (#32637904)
> might it have been the same amount of effort just to introduce some strong types into Javascript itself?

TypeScript exists as a first step to adding types to JS. Some folks are currently trying to figure out a way to add type annotations that the JS engine will just ignore for now based on what we've learned from TS.

Re:The silent majority in software by golergka on Monday August 29, 2022 @08:46AM (#32637504)
> ...might it have been the same amount of effort just to introduce some strong types into Javascript itself?

And how would it be different?

I've been using TS since 2016, both for front and back-end, and I can count an amount of times the transpilation abstraction leaked on one hand (and most of these were when I just learning TS and worked on weirdly configured projects). Aside from these, I could just forget about the fact that it's transpiled to JS under the hood.

Re:The silent majority in software by danielvaughn on Monday August 29, 2022 @08:49AM (#32637529)
Interesting, maybe it's just been my personal experience. I often work at the native DOM level, where the API is extremely forgiving and dynamic. The type system does not work well there, as I found several instances where the underlying type definition didn't match what the native API actually had available. My only recourse in those moments was to either use `any` or to write my own type definition (which I am not going to do).
Re:The silent majority in software by Existenceblinks on Monday August 29, 2022 @11:33AM (#32639475)
Reminds me of javascript the good part thin vs thick book meme. TS the good part is thin as well, though no one sets rules how many features they should use. Unsound type is bad enough, too many features make it worse. I wish they delete TS features, not trying to add more things to already broken underlying language (js)