Live data from Hacker News

JavaScript libraries should be written in TypeScript

staltz.com

121–130 of 285 posts

Re: JavaScript libraries should be written in TypeScript

#121
I wouldn't say that writing them it TypeScript would be good. But rewriting them in TypeScript at some point of their development might be good. Might expose some bugs (even some conceptual ones).

TypeScript doesn't feel like a boon when you write things with it because avoiding type bugs is further on the list. On the top of the list when you are writing something is figuring out what you actually want to write and what should be the APIs everywhere. TypeScript might make it harder to figure that out because you are getting invested too early in the shape of your interfaces. You'd get just yet another Java lib if you started writing in TypeScript. But once you have the right APIs there's no harm in formalizing them with bit of types.

Re: JavaScript libraries should be written in TypeScript

#122

Absolutely not. This is the opposite of what you should do in my opinion. If you're writing an API to be consumed in language X then you need to write the API itself in language X. This will help you capture and handle edge cases, language idiosyncrasies and other similar issues the way you want. Using a different language that gets transpiled into a target language also increases your surface area for bugs because n…

I disagree. The last letter of the API stands for "interface", which by the definition (a point where two systems, subjects, organizations, etc. meet and interact.) means you don't always have the ability to put a "same language" constraint on. By presuming the API will be consumed by something you have no control over makes you think about all the implications and types act as a guard of sorts.

Re: JavaScript libraries should be written in TypeScript

#123
post #72

Earlier quoted context omitted.

What does one miss out from Babel, given that TS is a superset of ES6?

It depends. The problem isn't so much what you might miss out on if all you are doing is writing a library, the problem is what limitations it places on consumers of your library. The most obvious Babel plugins are JSX and Flow. If your library is using TypeScript then it cannot go through the same build process* as any downstream project that is using any Babel plugin. That means you need to publish your library as…

I'm not sure I understand what you mean by "the client loses most the advantages of your library being in TypeScript in the first place." Your library still exposes a more clearly-defined API by making types explicit. And saying that the downstream consumer doesn't get the benefit of compile-time type checking if they've chosen not to use type checking is...kind of a truism?

Re: JavaScript libraries should be written in TypeScript

#124
post #93

Earlier quoted context omitted.

Typescript isn't all that differentiable from ES6. While I'm generally on the vanilla train, feeling fairly compelled to use it when messing around with angular2 has made me very much appreciate it. Change your .js filename to .ts and it will still run, which makes the typing just an added bonus.

Which leaves me wondering why I'd use Typescript over sticking with ES6, especially with tools like Babel making many future features available right now and libraries like React actively making use of them.

You shouldn't. My comment was in regards to readability of typescript vs coffeescript vs es6-javascript. If you don't want the added benefit of type-checking with minimal overhead, don't bother.

Re: JavaScript libraries should be written in TypeScript

#125
post #96

Earlier quoted context omitted.

> It's not about reading, it's about the tooling you need to set it up. npm install typescript -g This is the only addition on top of vanilla js, which is what you should be comparing with instead of the massive crap of build-tool-of-the-month. All said and done, if you're using those build tools in typescript, you'd be using them for javascript as well.

Wait, you mean I can start up a brand new Windows machine and type "npm install tsc -g" and stuff magically happens?

Visual Studio 2015 and Visual Studio Code both support npm. I don't even think you have to type "install", I think it's just a matter of listing "tsc" in your package.json file.

(I haven't tried it myself yet, but I just got back from VS Live in Vegas where the ease of package management in VS 2015 was brought up in several sessions.)

Check it out here: http://webtooling.visualstudio.com/package-managers/npm/

Re: JavaScript libraries should be written in TypeScript

#126
post #9

Earlier quoted context omitted.

How are you finding Nuclide? When I tried it a few months ago it seemed pretty slow and unstable, and eventually ended up consistently using all my CPU after a while. There wasn't a great deal of documentation/help setting it up available (true of Flow as well to a lesser extent), which was why I chose TS at the time, but am definitely interested in seeing how Flow is progressing.

I had the same problems when I tried it roughly the same time as you, but I tried it out again this week and it seems to be working much better now.

+1 I just give a second chance to Nuclide + Flow, and works great

Re: JavaScript libraries should be written in TypeScript

#127

Earlier quoted context omitted.

Well the classes are converted into functions and it adds members to the prototypes, right? Or is that ES5 only?

What you mean is indeed ES5 only. ES6 has classes so it doesn't need to convert anything.

Oh. Cool. I didn't know that.

Re: JavaScript libraries should be written in TypeScript

#128

I like static typing and I like dynamic typing. Gradual typing gives you the constraints of both without the benefits of either.

I'm sympathetic to the worst-of-both-worlds problem, but can this actually be true? In the case of types, constraints actually are the benefit, right? Do you mean that if you don't have compile-time assurance for 100% of the code, the value of whatever % type coverage you have isn't very useful?

As someone who's evaluating Typescript and Flow for a team of JS devs, my intuition is that having some typed code would give you beachheads of type safety, which seems like a reasonable win and an incremental path to improvement. I'm curious if this intuition is incorrect from the perspective of those who have used gradual typing in the real world.

Re: JavaScript libraries should be written in TypeScript

#129

Absolutely not. This is the opposite of what you should do in my opinion. If you're writing an API to be consumed in language X then you need to write the API itself in language X. This will help you capture and handle edge cases, language idiosyncrasies and other similar issues the way you want. Using a different language that gets transpiled into a target language also increases your surface area for bugs because n…

TypeScript is ES6 with types, so it is not a different language at all.

Re: JavaScript libraries should be written in TypeScript

#130

Absolutely not. This is the opposite of what you should do in my opinion. If you're writing an API to be consumed in language X then you need to write the API itself in language X. This will help you capture and handle edge cases, language idiosyncrasies and other similar issues the way you want. Using a different language that gets transpiled into a target language also increases your surface area for bugs because n…

Have you used TypeScript? Try it out for a month or two on a real project.

It's the best thing that has happened to web development in recent years - it improves Javascript in a completely compatible and future proof way.

The most amazing thing is that Microsoft created it. It's the polar opposite of Old Microsoft - standards are being followed (not subverted), it's simple pragmatic and has no lock-in.

Post reply on HN