Live data from Hacker News

JavaScript libraries should be written in TypeScript

staltz.com

51–60 of 285 posts

Re: JavaScript libraries should be written in TypeScript

#51
post #6

Or at the very least, library documentation really needs to clearly specify the types of parameters and return values. I'm positively surprised when I come across a JavaScript API whose documentation actually specifies this very basic information. Coming from the static typing world, it's astounding...

Yes. And, having worked with typescript, its quite annoying how poor most typings files are that you get from the definitelytyped repository. It's better than nothing, but they are unversioned (so always behind) and often very incomplete. I agree with the author: types are there anyway, typescript seems to work well enough and has traction now, so please, if you write a library: add those type annotations bottom-up i…

Or... at least accept the pull requests that have them cough async cough

Re: JavaScript libraries should be written in TypeScript

#52
post #14

Starts off with: > Typed vs dynamic is a rather controversial topic, so I’m not trying to spark that discussion all over again. Goes on to explain that typed is betterer... The article doesn't really offer up any real arguments as to why you should specifically use TypeScript, but really just says typed is nice and everyone lints anyway (not true) so the step up to a type system really isn't that big of a deal (also…

Context matters. Some people, while writing their own application code , find types to be annoying, a nuisance, or whatever. That's a personal (or team) decision. This article is targeted at authors and maintainers of high-adoption libraries. It says so right in the second paragraph. And, frankly (speaking as someone with experience on this topic), he's right. Maybe static isn't always the answer, but in the context…

He mentioned "contracts." You can throw off the shackles and use the `any` type internally, but at the very least your public interface should have a well-defined contract. TypeScript is one [very good] way to achieve that and it even spits out machine-readable "contract summaries" for you (.d.ts files).

Re: JavaScript libraries should be written in TypeScript

#53
post #25

I just hope this will not be yet another point of fragmentation for JavaScript. Not looking forward to not only having to use different build tools for all libraries I work with but also different pre-processors and type analyzers. Oh my.

Why would you? Just don't use those libraries that don't fit your work flow. This is where "framework fatigue" comes from, this word insistence on trying to include every project under the sun into your project.

Re: JavaScript libraries should be written in TypeScript

#55
I'm not certain I agree. I agree with better type systems, but does anyone remember ECMAScript 4? Which Microsoft vetoed after Macromedia had gone ahead and adopted it as ActionScript 3 (coincidentally around the time they were trying to get everyone to adopt Silverlight). Well that's basically what TypeScript is (with slightly more advanced type support). It's a nonstandard dialect of JavaScript. Something that everyone (especially MicroSoft) ostensibly agreed was not what was wanted or needed.

The alternative to that of course is Babel. Which is standard JavaScript, mostly. Including the draft stuff that isn't ratified yet. But you don't have to use any of that if you don't want. And things like Flow and JSX, which are just plugins so again, you can take them or leave them.

The authors argument eventually distills down to a choice between TypeScript and Flow. He concludes that TS is the best choice because it has the momentum, but that's a false choice. The choice is between Microsoft's TypeScript transpiler and Babel.

If you use Babel you can choose to use Flow, but if you use TypeScript you lose out on everything else Babel has to offer.

And outside of the .Net+Angular world, Babel has all the momentum.

Re: JavaScript libraries should be written in TypeScript

#56
This does not solve the problem of bad documentation or bad libraries. Points to make his case are weak. Better autocomplete? Reliable libraries? The library still compiles to JS and you can still use TypeScript naively and incorrectly and ignore typesetting altogether.

TypeScript doesn't solve the problem at all. It merely adds another layer of complexity/knowledge to a Library that still compiles to JavaScript. Write it in JavaScript, deliver it in JavaScript. If your documentation and implicit typesetting is giving you pains, then you should revisit what you have written and how you've built your library. Not switch to something that tries to enforce better behaviour.

Re: JavaScript libraries should be written in TypeScript

#57

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…

Couldn't agree with this more. Specifically, the lost hours due to CoffeeScript.

Re: JavaScript libraries should be written in TypeScript

#58
post #23

I still run into libraries written in Coffeescript, and hate it when I do, because I don't use it and have a hard time following it. Javascript libraries should be written in Javascript. Worship the one true God!

Except this really isn't an issue for TypeScript, it reads that close to JavaScript.

Except it really isn't. As soon as you convert to .ts you'll get a boat load of errors and have to re-declare your types and input parameters as vars for TS to ignore them. Also not sure what would be the point of using pure javascript in this context other than marginal benefit of a compiler detecting javascript syntax errors. Not arguing pro or against TS just pointing out it's not the same as using javascript

Re: JavaScript libraries should be written in TypeScript

#59
post #7
post #3

Earlier quoted context omitted.

What's a "big" JavaScript library? How do you know how widely adopted your JavaScript library will be?

Regardless, it's difficult to think of any situation where a All _______ should use _______ is really justified.

It's another click baity title like "Why we stopped using [popular X] for [less popular Y]", "[Popular X] considered harmful", "Why you shouldn't use [industry standard X]" etc.

Re: JavaScript libraries should be written in TypeScript

#60
post #23

Earlier quoted context omitted.

Except this really isn't an issue for TypeScript, it reads that close to JavaScript.

Maybe we'll agree to disagree but this [0] doesn't read very close to Javascript at all. [0] https://github.com/Microsoft/TypeScriptSamples/blob/master/t...

That does though. Skip to line 120 where code is actually written, and it's ES6. The top part is type definitions, which could just as easily be thrown at the bottom. I'd argue it should for exactly this reason.
Post reply on HN