Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> that will ensure that these situations no longer happen?

Well... but Typescript doesn't ensure those situations won't happen, ironically because it's a superset of Javascript and it's not strict enough. Typescript is just documentation that compiles. If a library has bad documentation, it's likely to have bad Typescript as well, and bad Typescript can be a giant curse.

Typescript will happily allow you to force a typecast that doesn't reflect reality -- it will happily allow you to say that a method always returns a string when sometimes it returns a number, and it will happily compile in those scenarios.

Tracking those bugs down are a giant pain. I don't hate Typescript, but people who look at this and think, "I don't have to care about what libraries I'm using, I can just trust the interface" are fooling themselves. I've debugged plenty of errors in Typescript code that ended up being the result of me trusting an interface that didn't reflect the reality at runtime.

There's value in Typescript for some projects, but it does not remove the need to be careful about documentation and dependencies, because it's so easy for both dependencies and downstream code to circumvent the protections Typescript gives you.



> it will happily allow you to say that a method always returns a string when sometimes it returns a number

Just replace "say" with "assume" and you have the javascript situation: javascript is just typescript with documentation stripped off.

> Typescript is just documentation that compiles.

Exactly. And we're arguing whether code should contain documentation or not.


This is kind of a subtle idea, but in some instances, the illusion of safety can be harmful. It is occasionally (not always) better not to give people the illusion of safety when that safety doesn't actually exist. Sometimes putting an ineffective railing along a mountain path increases accidents, because it's human nature for people walking on that path to watch their footing less when they see the railing.

>> Do not use random libraries. A good library should have even some basic docs outline its API and how to use it.

> A whole lot of the time, especially in Javascript land you will join projects where libraries are being used, and where documentation is lacking. Your advice here is basically to not be part of those projects? Instead of developing and using tooling that will ensure that these situations no longer happen?

I'm not here to hate on Typescript, I think it has its uses. I use Typescript in some places. I generally avoid it for personal projects, but I would probably encourage it for larger orgs.

But the idea that Typescript protects you from needing to care about interfaces is exactly the problem with over-reliance on Typescript, and is the source of a lot of really badly written code that treats interface design like an afterthought. If you're using Typescript for documentation and you're treating it like a tool, you can write some really elegant code, you can catch some shallow bugs around types and assumptions, you can integrate your documentation into your IDE. If you're looking at Typescript and saying, "this is an efficient way for me to document what my code does", then this is all very good.

But if you're using Typescript and saying, "now I can pull in any dependency I want", then Typescript is doing you more harm than good.

The specific advice orange gives in those two sentences is good advice. If Typescript is giving you a false sense of confidence about using random libraries, then that's a real negative.

I use both pure JS and Typescript almost daily, and I consume plenty of dependencies written in Typescript. I still occasionally need to debug the stack traces in those dependencies. I still occasionally need to read through their source code to figure out what they're doing. Badly documented dependencies are not a problem that Typescript solves.


> in some instances, the illusion of safety can be harmful

> you can catch some shallow bugs around types

> If Typescript is giving you a false sense of confidence about using random libraries

> Badly documented dependencies are not a problem that Typescript solves.

Seriously, what is your claim? That if you have a badly documented third party library with wrong typings then typescript doesn't help? Well, that's some news! Try the same library with no documentation at all then, so you won't get a false sense of safety. If that is your problem, why don't you just avoid importing typings for libraries?

And I don't get this insistence on bugs and safety. Sure, typescript probably prevents you from doing really stupid stuff sometimes, but that's not the main reason I use it. I use it because it both allows me to write much more complex code and speeds up dramatically the development- because the tool takes on itself what is otherwise a heavy cognitive load: remembering and inferring interfaces and signatures that are there all the time, even if implicitly.


> If that is your problem, why don't you just avoid importing typings for libraries?

Yes? That's exactly what people are doing when they import a dependency written in Typescript into a pure JS project.

> A whole lot of the time, especially in Javascript land you will join projects where libraries are being used, and where documentation is lacking. Your advice here is basically to not be part of those projects? Instead of developing and using tooling that will ensure that these situations no longer happen?

My claim is that Typescript does not solve the problem of joining a project with poorly written, undocumented libraries.

I'm not bashing on Typescript. If the primary way you use it is as an organizational tool to reduce cognitive load and make interfaces more explicit, then that's great. You're using Typescript as intended. And there are some scenarios where a compile-time type-checker is just obviously the right tool for the job.

You started this thread by asking how JS devs could manage 3rd-party interfaces without Typescript. I assume you genuinely want an answer to that question. The answer is that for some people on some projects, using well defined interfaces and keeping a reference page open with documentation is about as fast and about as easy as using Typescript, and that following that process doesn't increase their bug count or cognitive load to the point where they feel the need to introduce a compilation step.


> Yes? That's exactly what people are doing when they import a dependency written in Typescript into a pure JS project.

Except that in this way they also lose good typings for any other library and for their own code as well. I understand an outdated definition file can be annoying, but there can be various ways to only ignore type definitions for that specific library.

> My claim is that Typescript does not solve the problem of joining a project with poorly written, undocumented libraries.

That is a problem that no language or tool can solve.

> You started this thread by asking how JS devs could manage 3rd-party interfaces without Typescript.

No, I didn't, you're confusing me with someone else. Inertiatic asked at the beginning of this thread

"Who are these mythical people? [the developers who never make typings mistakes] Do they just magically decipher what interface an argument conforms to? What methods are accessible on a returned result from any random library they're using?"

Then the thread proceeded by 1) ignoring the first part of his question, about having to remember signatures in general, including of your own code; 2) interpreted "random" in a strictly negative way, as if it meant "any junk library" instead of "every library".


> you're confusing me with someone else.

Apologies. That actually explains a lot :)

> 1) ignoring the first part of his question, about having to remember signatures in general, including of your own code; 2) interpreted "random" in a strictly negative way, as if it meant "any junk library" instead of "every library".

The answer to 1) is that remembering type signatures in general (or primarily referencing them through a separate spec sheet) turns out to be about as easy as Typescript for some projects and some people. The answer to 2) is that not "every library" is complicated enough to need extensive bindings, and that since Typescript doesn't help you with junk libraries anyway, the benefits to well-documented libraries turn out to be relatively minor.

I mean, I don't even use an IDE for development. I'm on Vim/Emacs, so the immediate documentation benefits of Typescript you describe literally just don't apply to me. And there are plenty of JS devs in the same position, where their only interaction with the type checker will be during a compile.

The short answer to how people magically know the type signature of random methods is they don't. They use documentation, or they look at source code, which is something I still regularly have to do in the projects where I use Typescript.

----

> Who are these mythical people? [the developers who never make typings mistakes]

In general, typings mistakes aren't a major source of bugs in most of my projects. I only observe them to be a major source of bugs in large orgs where lots of teams are coordinating on large interfaces. And that's not because I'm a genius programmer or have a magic power, there are normal programmers who will find themselves in that situation.

> Do they just magically decipher what interface an argument conforms to?

No, I look at the documentation. In general, I also spend a lot of time on interface design so my interfaces are small enough and consistent enough that I can hold the entire API in my head without much cognitive load and without needing to check documentation very often.

> What methods are accessible on a returned result from any random library they're using?"

I look at the documentation, or the source code if I don't trust the documentation.

You are always going to be able to make the argument that I just don't understand how much time I could be saving. I don't know how to prove to you that this isn't a massive drop in productivity for me, in the same way that I don't know how to prove to you that it's not a massive drop in productivity for me to write DOM manipulation code in Vim instead of Webstorm with tab-completion. All I can say is, I use both JS and Typescript on an almost daily basis, and I pay attention to the cognitive loads, bugs, and time-sinks I have in each environment. The JS environments aren't any slower to program in.


> javascript is just typescript with documentation stripped off.

JS, at its core is a dynamically typed, functional language. Typescript aims to model a strongly typed, OPP language. The two programming paradigms have their own strengths, weaknesses and most importantly: patterns and best practices.

If the only difference between your pure javascript code and your typescript code is the annotations, you are doing it wrong.


> Typescript aim to model a strongly typed, OPP language.

Do you think? I just take TS for what it gives me, documentation and static code analysis. I can write down my knowledge and expectations about the code and have the tool keep track of it for me, removing the cognitive load of doing it myself- which I'd have to otherwise.

Can you give me an example of a pattern that is "good" in JS and not in TS, or the other way round? I'm curious.


A good indirect example is an actual feature added to JS recently, which are classes. Classes are the main unit of OOP, while functions are the main unit in the functional paradigm. The class syntax in JS is not a real new structure added to the language, but just syntax sugar. Underneath it all, the JS compiler just converts it to a regular JS function using a specific pattern. You could, if you wanted, save the compiler that work by just coding using that pattern to begin with.

When using TS the class syntax fits in perfectly for me, an in pure JS I'd stick to functions.


So you're giving a javascript feature as an example of a pattern that doesn't fit javascript. Then you claim to use an equivalent but syntactically different form in js? How does this constitute a different pattern?


> Typescript aims to model a strongly typed, OPP language

That's a rather puzzling statement, to be honest. Strong type system is explicitly one of the non-goals[0]. I write typescript code everyday and most of it is functional. Things like fp-ts[1] make it even more enjoyable.

[0] https://github.com/Microsoft/TypeScript/wiki/TypeScript-Desi... [1] https://github.com/gcanti/fp-ts


Am comfortable using both OOP and functional styles, though I do enjoy the flexibility and terseness that can be produced from the functional approach.

What I meant by the comment is that when using TS, I just find it more easy and natural to go with the proper OOP approach. I think the very existence of a separate library to enable a more functional style in TS proves this goal has not been met yet.


Functional languages usually have extensive standard library, while js does not. Adding it is not really a goal of ts.


It's not very strongly typed at all, and it absolutely doesn't force you into the OOP paradigm.

Why don't I consider it strongly typed?: Structurally subtyped as opposed to nominal typing, the pervasiveness of any, allowance for vanilla JS coercion, exceptions and promise rejections are untyped, etc.


I agree, that's why I said TS aims to model a strongly typed languange.


Typescript won't save anyone from intentional writing bad code or using a broken library. Nothing will. I don't see how that's a criticism of TS itself.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: