News for hackers. Stuff that matters.
Re:Types are the basic tool of software design by seanwilson on Friday January 03, 2025 @10:29AM (#42586457)
I love this before/after example of TypeScript being removed from a library as a clear illustration that only occasional simple type annotations are needed: https://github.com/hotwired/turbo/pull/971/files

Looks like so much useful documentation for understanding the code, assistance for refactoring, and static checking (which are like always running, fast and exhaustive unit tests for free) for a modest amount of type annotations that each person on the team would need to recreate in their heads anyway. Static type checking is so useful for catching runtime errors to do with optional values and index/key not found error too, that always create a load of edge cases.

Editing JavaScript with no types is scary, especially when you didn't write it. You're mostly guessing what the types must be and hoping your running code exercises enough edge cases to catch any problems. "You should write more tests" as an alternative rings hollow for me because (for the things they will check for you) types are more concise, exhaustively check all values, are ran continuously, are fast, aid with automatic refactoring, document the code in the same file its in, and will locate exact snippets that are causing errors. More often than not, test mostly check happy paths, aren't even close to as exhaustive, and nobody is writing tests that check every combination of function parameters being null vs non-null, string vs number etc. (which would be a huge amount of noise in your test suite that would slow down refactoring too). Types are a compliment to tests as well, not an alternative.

I can't relate to arguments about types getting in the way (as long as you avoid trying to get too clever like with complex generics and TypeScript conditional types). Usually when this happens, it's because it's hard to reason the runtime edge cases in your own head and there's probably a simpler way to write it. I'd love a good non-niche example to the contrary. Worst case in TypeScript, you can use `any` as an escape hatch anyway so as long as you can add types to most of your program it's not a strong reason against for me.

Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @11:01AM (#42586749)
That PR is great. Not only do they get rid of TypeScript, they get rid of prettier and some gratuitous trailing commas. https://github.com/hotwired/turbo/pull/971/files#diff-865b8f...

I’m not joking. I prefer the after.

Edit: thanks. Yes, commas, not comments.

Re:Types are the basic tool of software design by madeofpalk on Friday January 03, 2025 @12:11PM (#42587410)
I can't imagine caring that much about trailing commas. The point of a tool like prettier is to not sweat such irrelevant details.
Re:Types are the basic tool of software design by slooonz on Friday January 03, 2025 @12:33PM (#42587651)
Trailing commas actually have an important role.

Let’s say you have some code :

  const foo = {
    x: 1,
    y: 2,
  };
and you want to add a field :

  const foo = {
    x: 1,
    y: 2,
    z: 3,
  };
Without trailing commas, the diff is

  - y: 2
  + y: 2,
  + z: 3
With trailing commas :

  + z: 3,
The second diff is actually much clearer at what’s happening.

This is why it irritates me when people forget the trailing comma. It’s not a problem in the moment. But I know it will be a small but avoidable cognitive burden on the next PR (wait, why did "y" changed ? Oh, right, it didn’t).

Re:Types are the basic tool of software design by Jarwain on Friday January 03, 2025 @04:10PM (#42589500)
I also like it because I can reorder items in an array without having to also add/remove a comma!
Re:Types are the basic tool of software design by fire_lake on Friday January 03, 2025 @12:47PM (#42587798)
Maybe diff should change to suit programs, rather than programs change to suit diff.
Re:Types are the basic tool of software design by slooonz on Friday January 03, 2025 @02:36PM (#42588756)
You want diff to know the syntax and semantics of every programming language out there ?
Re:Types are the basic tool of software design by knubie on Friday January 03, 2025 @02:53PM (#42588889)
This is possible already with tools like difftastic.

https://github.com/Wilfred/difftastic

Re:Types are the basic tool of software design by DarkPlayer on Friday January 03, 2025 @04:27PM (#42589655)
Difftastic would not solve the issue described by madeofpalk because it still highlights the added comma. You need a diff tool that can distinguish between optional and required syntax. So far I am not aware of any tool that supports this, except the one I am working on (SemanticDiff).
Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @06:23PM (#42590585)
I'm pretty sure this counts. https://github.com/afnanenayet/diffsitter

Certainly the idea has been suggested many times. I think people end up formatting both before/after and doing a diff on formatted before against formatted after. I've done that.

Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @02:48PM (#42588852)
It would be like a syntax highlighter. It’s doable.
Re:Types are the basic tool of software design by slooonz on Friday January 03, 2025 @03:50PM (#42589346)
But diff does not exists in a vacuum. It would need to be integrated to IDEs, editor, merge tools, PR tools. You now have to have `patch` understand and depend on the details of the syntax of your language. In a way that may break between different versions of diff and patch. Not even starting with variants of a language, different interpretations of how to handle the preprocessor/macros system, different editions of the same language.

All that, just to not have to add a trailing comma ?

Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @04:30PM (#42589688)
Most things like patch would still use line diff. I think I would want this semantic diff in the merge request review and the logs. It could have definitions distributed with the syntax highlighters in language plugins in an editor/IDE, git CLI, and git forge.
Re:Types are the basic tool of software design by from-nibly on Friday January 03, 2025 @07:41PM (#42591069)
So GitHub, git lab, gutbucket, gittea (and its 50 forks), fossil, and all the other tools should change, not the formatting of the code?
Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @08:20PM (#42591353)
No, having a concise separator is still preferable to saving a line in a diff.
Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @01:14PM (#42588048)
Indeed. That’s why prettier ain’t pretty.
Re:Types are the basic tool of software design by madeofpalk on Friday January 03, 2025 @02:57PM (#42588922)
Sure, I can get behind that. That's why we just automate these trivial things away.
Re:Types are the basic tool of software design by Shacklz on Friday January 03, 2025 @11:26AM (#42586970)
Funny, I have exactly the opposite feelings.

I've worked on dynamically typed code-bases in the past, and also on code-bases without formatters like prettier... and I'm not going back there.

So many odd bugs can be avoided simply by taking a little bit of time and declaring some types. It doesn't have to be a super fancy type-system (I actually prefer if it isn't), but I really want to know whether something is expected to be a string or a number, or what properties an object can be expected to have, or what values a literal can accept, and so on. It's just so frustratingly time-consuming to figure out in hindsight. And all for some person's odd perception of simplicity.

Same for formatters - all the squabbling over some personal preferences on how code should be formatted. Hell no. Let's install some opinionated formatter and let it do its job, I want to tackle more interesting problems than yet another narrow-minded discussion about necessity of semicolons, placement of whitespace or line-length.

I'm at a point where I straight-up refuse to work with people who prefer to work that way; or at least require them to be a few departments away from me. They can be programming gods; if they can't be arsed to type their code so I can immediately grok it, they can program elsewhere.

Re:Types are the basic tool of software design by n144q on Friday January 03, 2025 @04:04PM (#42589452)
Same.

I sometimes write TypeScript for 2 hours straight without actually running the code or any unit test, and it would just work -- the return value is exactly what you want. I don't think I can do that for JavaScript for more than 15 minutes even though they are the "same language".

Re:Types are the basic tool of software design by vallode on Friday January 03, 2025 @11:19AM (#42586889)
I could not disagree more, but I am obviously open to it being a personal preference. I've spent months updating JavaScript codebases that are between 3 and 5 years old, most of them written with vanilla JavaScript. A _few_ of them were written with TypeScript at least partially and those files were the least mentally taxing to upgrade.

Some of the codebases had tests, a good chunk of which could (and were) eliminated simply by using TSC as part of the build process. I think allowing the compiler and tooling like prettier to make choices for you leaves more space for you to think about the problem, not the myriad of problems _around_ it.

Re:Types are the basic tool of software design by PittleyDunkin on Friday January 03, 2025 @11:27AM (#42586975)
What's the benefit of removing the explicit typing? It strikes me as hamstringing readability of the code. This just feels like giving up and calling the status quo "beautiful" or something.
Re:Types are the basic tool of software design by bnetd on Friday January 03, 2025 @11:29AM (#42586991)
To reinvent it.
Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @01:39PM (#42588290)
People tried to argue that in the PR, suggesting to change variable names.

This one: https://github.com/hotwired/turbo/pull/971/files#r1317386731

> function buildFormData(formElement: HTMLFormElement, submitter?: HTMLElement): FormData {

> It might be useful to know that submitter is optional argument here.

> How would one express that? The variable now needs to be renamed?

And two lines later:

> const name = submitter?.getAttribute("name")

It's already easy to see that submitter is optional.

Re:Types are the basic tool of software design by lelandbatey on Friday January 03, 2025 @05:16PM (#42590038)
Only easy to see that it's optional if you read the source code vs. looking at the auto definition in your IDE/in autogenerated documentation.
Re:Types are the basic tool of software design by seanwilson on Friday January 03, 2025 @06:18PM (#42590542)
> It's already easy to see that submitter is optional.

Why force yourself to read inside function implementations like this for every function you might use when fool-proof automated assistance is right there? What if the function was tens of lines long? What if the optional value only appeared inside a chain of 5 nested functions? How would you easily fix/update your code if the value isn't optional now but got changed to be optional in the future?

Manual effort like this is error-prone, not scalable, and a huge distraction. Why would avoiding type annotations be worth this?

Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @07:00PM (#42590829)
Because this is an internal function in a _library_, and you should be willing to look inside it if you're changing a call to it.

Here's where whether or not it's defined can be traced to: https://github.com/hotwired/turbo/blob/41c074ff113a8882aadbc...

Maybe the types should have been written differently, to indicate that it's the type of the submitter property in a FormEvent. That's another cost of using TypeScript, is trying to get the types right. TMTOWTDI.

Re:Types are the basic tool of software design by seanwilson on Friday January 03, 2025 @08:57PM (#42591588)
> Because this is an internal function in a _library_

Why does that matter? Error-prone approaches create buggy code and we don't want buggy code, whether it's a library or something else. Libraries can be huge and complex, and arguably should have more robust edge case checking than regular code.

Re:Types are the basic tool of software design by knallfrosch on Saturday January 04, 2025 @04:03AM (#42593555)
You're only able to see it by reading the function body.
Re:Types are the basic tool of software design by superb_dev on Friday January 03, 2025 @11:52AM (#42587228)
Even with the find/replace errors that seemingly got merged? This PR feels like it was so rushed
Re:Types are the basic tool of software design by lofenfew on Friday January 03, 2025 @11:17AM (#42586878)
s/ent/a
Re:Types are the basic tool of software design by motorest on Friday January 03, 2025 @12:18PM (#42587482)
> I’m not joking. I prefer the after.

Everyone likes the simplicity that comes with removing guardrails, but when easily preventable problems happen that's glanced over as an unavoidable fact of life.

Re:Types are the basic tool of software design by knubie on Friday January 03, 2025 @02:58PM (#42588934)
> but when easily preventable problems happen

easily preventable is doing a lot of work here. This is assuming the types are already written and never need to be maintained. How often does the type checker catch a problem with the code vs the type definitions themselves?

Re:Types are the basic tool of software design by seanwilson on Friday January 03, 2025 @05:38PM (#42590242)
> How often does the type checker catch a problem with the code vs the type definitions themselves?

All the time? I've worked on code bases with multiple people without types, and whenever there's a place that can produce e.g. "string or undefined" like when a key might not be found in a collection, it produces edge cases everywhere that don't get noticed outside of the happy path, including after the code has been merged. You're forced to simulate in your head all the code paths, and remember which values might be optional which is error-prone, isn't scalable and eats up brain cycles.

Even typos like `object.detail.name` vs `object.details.name` cause runtime errors, or calling a function with the wrong number of arguments or in the wrong order, some of the most trivial bugs you can think of.

I don't understand how someone can dislike writing type annotations so much that they're happy having their time wasted like this when automated assistance is right there. To me, it's like not wanting to use a spellchecker when writing an article, or opting out of syntax checking code before it's ran.

I get that problems with type definitions can be frustrating but it's better than runtime errors (having to write more tests isn't better either), and often flags actual bugs, potential future bugs ("what if this was undefined sometimes?"), or overly dynamic code that's hard to reason about (e.g. conditionally adding/removing fields from objects, or having a field that changes type between string/array/undefined vs using an array of strings instead). I'd love a counter example here - often the examples I see has overly dynamic code because the coder hasn't had a strong incentive to avoid such code before. As in, I write type checker friendly code so I can benefit from type checking.

90% of type annotation stuff I work with is mundane and simple (e.g. strings, numbers, arrays, something optional, object with basic fields) and is well worth the minimal effort. The more complex stuff isn't needed often, usually optional, and gives you protection from tricky bugs too.

Re:Types are the basic tool of software design by knubie on Friday January 03, 2025 @08:33PM (#42591434)
> Even typos like `object.detail.name` vs `object.details.name` cause runtime errors, or calling a function with the wrong number of arguments or in the wrong order, some of the most trivial bugs you can think of.

In Clojure, a dynamically typed language, referencing a var that is not declared, or calling a function with the wrong number of arguments will result in a compilation error. You don't need static typing or type annotations for this.

> I don't understand how someone can dislike writing type annotations so much that they're happy having their time wasted like this when automated assistance is right there. To me, it's like not wanting to use a spellchecker when writing an article, or opting out of syntax checking code before it's ran.

It's not about liking or disliking, it's about "wasting time." Spell checking and syntax checking are free, annotating everything with types and maintaining them is not.

Re:Types are the basic tool of software design by seanwilson on Friday January 03, 2025 @09:09PM (#42591652)
> In Clojure, a dynamically typed language, referencing a var that is not declared, or calling a function with the wrong number of arguments will result in a compilation error. You don't need static typing or type annotations for this.

Can it catch problems like e.g. if you try to access a `y` field on some input object but only field `x` is defined? And if you want to define a function that takes a callback that must accept two arguments? What about ways to force you to check a variable with type `string | undefined` is defined before you do something with it? At some point it must breakdown because more complex checks can't be inferred?

> it's about "wasting time." ... annotating everything with types and maintaining them is not.

See this example. I'd argue the type annotations are minimal, but you gain a lot of safety, documentation and refactoring help from them: https://github.com/hotwired/turbo/pull/971/files

Re:Types are the basic tool of software design by benatkin on Friday January 03, 2025 @09:32PM (#42591792)
The arity check in clojure sounds like a nice balance between runtime and compile time errors.

Having runtime errors is the point - that's real dynamic typing for you. If not for the possibility of runtime errors, you'd have to do more to have the code run. A lot of junior devs who prefer static typing don't get it. Senior devs who prefer static typing at least understand that some have a different preference. Of course you don't want a lot of runtime errors to make it to production, but in development, yes.

There are those who think that the IDE should provide feedback at every keystroke. Most IDEs have this opinion built into them. But I don't like it very much. It's gotten to the point where there isn't an IDE I really like. I miss Atom.

Re:Types are the basic tool of software design by Tainnor on Saturday January 04, 2025 @05:05AM (#42593809)
> To me, it's like not wanting to use a spellchecker when writing an article

Huh, I'm absolutely in the pro-static-types camp, but I never use spellcheck which I personally find distracting and unhelpful. I wouldn't consider those two be similar issues at all.

Re:Types are the basic tool of software design by hacked_greenhat on Wednesday January 08, 2025 @01:43AM (#42631657)
> I'm absolutely in the pro-static-types camp

Reflecting on your comments on the Transcendental Syntax threads, you should really look into linear logic, or at least System F (https://en.wikipedia.org/wiki/System_F).

Re:Types are the basic tool of software design by Tainnor on Wednesday January 08, 2025 @07:04AM (#42633313)
Definitely will at some point
Re:Types are the basic tool of software design by hacked_greenhat on Wednesday January 08, 2025 @03:38PM (#42638202)
me too!
Re:Types are the basic tool of software design by motorest on Saturday January 04, 2025 @02:14AM (#42593088)
> easily preventable is doing a lot of work here.

It isn't. It's the most basic value proposition of TypeScript.

> This is assuming the types are already written and never need to be maintained.

No, it doesn't.

You need to write software so that you get an application that does what you wish it to do. Does this surprise you? This is not something that was invented by TypeScript.

When a software developer wants to prevent bugs when adding features, what they do is add preconditions and post-conditions to enforce invariants, and they handle code paths specific to both the happy path and failure modes. What static typing does is ensure these checks can be done at compile time, and that failure modes can be prevented by ensuring data types that do not meet conditions cannot be passed to functions.

TypeScript allows developers to check preconditions and post-conditions by defining types and adding type assertions that verifies whether an object is of a certain type, and then assert at compile-time if they meet preconditions and post-conditions. If they specify a type that has specific characteristics, the typescript compiler helps them by automatically checking preconditions and post-conditions, and prevent code paths that are proven to be failure-prone.

This is the very basic of software development. This can only be interpreted as "maintenance" or "extra work" by someone who thinks it's ok if the program crashes and malfunctions if preventable failure modes are not prevented. This is not a trait of a programming language. This is a trait of your and the level of quality at which you do your job.

Re:Types are the basic tool of software design by plagiarist on Friday January 03, 2025 @11:26AM (#42586963)
> This one is interesting. I would have no idea what a "submitter's" purpose is here with or without Typescript.

> I am excited to see how this code base changes without Typescript. The "types" or what things are will have to be communicated in some way. This may lead to very readable code.

This is funny to me. "If we don't have the compiler checking types the developers will perfectly compensate with documentation, an improvement." Maybe remove the unit tests as well, then the developers will write accurate code.

Re:Types are the basic tool of software design by marcosdumay on Friday January 03, 2025 @04:22PM (#42589608)
It's using that expendable developer time to free some precious extra developer-machine-CPU time.

Who wouldn't want to spend some weeks or hard labor so their computers can work for a few less seconds? Let's keep what's really important in mind!

Re:Types are the basic tool of software design by Tainnor on Friday January 03, 2025 @08:37PM (#42591464)
That PR reminds me why I'm glad that I left the Rails world, despite multiple HN threads recently touting its benefits. Ultimately, DHH just decides things on a whim and ignores everyone's objections.