Live data from Hacker News

JavaScript libraries should be written in TypeScript

staltz.com

111–120 of 285 posts

Re: JavaScript libraries should be written in TypeScript

#111
post #101

People spending their free time writing open source software should write it in whatever they want. There is no way anyone should be forced to comply with any standard when they are freely sharing their code. This article perfectly reflects my thoughts when it comes to asking open source authors to do anything: http://hueniverse.com/2016/01/26/how-to-use-open-source-and-...

This arguments boils down to "they are free persons" which is besides the point and doesn't tell us anything.

This is not about where you start from (totally free, their own time, can do anything) nor about whether they can be forced to do X or Y.

It's about what kind of community and software they (we) want to build, and what would be the best practices and suggestions.

Developers aren't forced to package their libs for npm distribution, write comments, ensure they play well with latest browsers, etc. either, but they do -- not because they are forced to, but because they agree that it's a nice thing to do.

FOSS developers do all sorts of stuff to make their software better for users already -- writing documentation, packaging for various platforms, evangelizing it, conforming to certain standards even when they don't personally use them or care for them etc.

Software development, especially community OSS is not about proving how "individual" one is, and doing everything for fun. It's also about wanting to contribute to a common cause and build some common foundations and best practices for a platform or language etc. Most projects agree to that idea, or at least pay lip service to the idea.

In this case, the TFA is not about forcing anyone to do something they don't want, so your reaction is besides the point. It's about suggesting what the author thinks is the better way for developers to do going forward.

>There is no way anyone should be forced to comply with any standard when they are freely sharing their code.

Force them not, but nudge them towards complying, or convince them to comply, yes, there are TONS of ways. Advocacy for some specific practice, like TFA does, is one.

Re: JavaScript libraries should be written in TypeScript

#112

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…

At work our entire front end is TypeScript, and thanks to source mapping I've never had to look at JavaScript even when debugging problems. Typescript simply adds type annotations so you can get error underlining when you misuse a variable, and even more importantly (productivity wise) you get auto-complete members as you type.

Don't want types? Fine don't use them, and you have JavaScript code again.

Re: JavaScript libraries should be written in TypeScript

#113
How so? Anyone can contribute to javascript but only a very small subset of javascript developers can contribute to the typescript library. Javascript will be around and in vogue as long as it's the ubiquitous browser language. Typescript? Who knows - some other language transpiling to js might be in vogue in a few months and then nobody would be around to maintain your libraries.

Re: JavaScript libraries should be written in TypeScript

#114
post #67

Earlier quoted context omitted.

I can absolutely understand that coming from coffeescript (optional parentheses resolution... yikes). But TypeScript is basically "Javascript + types + ES6". They call it an "erasing compiler" because it's not meant to do much but remove types/make ES6 code work with ES5. There is one gotcha in name resolution when you're working in modules (if you are in a module a.b, and a.c exists, then c will automatically refer…

With `--target=es6` I'm not sure if the TypeScript compiler does any code transformation other than removing type annotations. So the chance of a 'transpiler error' is basically zero.

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

Re: JavaScript libraries should be written in TypeScript

#115

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 ever…

I don't think you understood the point of the article - it was targeted at library maintainers, not all users. Types often end up a part of the documentation, and internal typing is often needed in libraries to keep track of model state, so having it at the foundational level in the library itself adds tremendous benefit.

Yes, I don't think I was clear enough but I edited a lot of points out of my post as it was far too long!

The biggest problems in any modern, large-scale JavaScript project are the build process and dependency management. If you are publishing a TypeScript library then it can be consumed either natively as TypeScript, or as ES5.

If it is being consumed as ES5 then your consumers lose most of the benefits of it being in TS. For them to consume it as native TS puts a fairly hefty constraint on their build process, namely that it must go through the TypeScript compiler.

That doesn't negate the internal benefits to your library, but Flow is another option that is a better fit if you are mostly interested in going with the prevailing wind. After all, that was the author's central argument as to why you should choose TS over Flow, and IMO it is quite wrong.

Re: JavaScript libraries should be written in TypeScript

#116
post #23

Earlier quoted context omitted.

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

It's not about reading, it's about the tooling you need to set it up. JS initial strengh was ease of dev for beginers. Throw in a transpiler, a dependancy manager, a package manager, a bundler with FS watching, and source maps and what you got is wall for the very same type of dev that were producing all those fantastic jQuery plugins 10 years ago. And actually even for me. I do know how to install and use all those.…

You don't need tooling to use libraries written in Typescript. NPM libraries distribute the compiled source.

Re: JavaScript libraries should be written in TypeScript

#117
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...

Do you have an example which has stood out for you?

Re: JavaScript libraries should be written in TypeScript

#118

How so? Anyone can contribute to javascript but only a very small subset of javascript developers can contribute to the typescript library. Javascript will be around and in vogue as long as it's the ubiquitous browser language. Typescript? Who knows - some other language transpiling to js might be in vogue in a few months and then nobody would be around to maintain your libraries.

If you know JavaScript, I honestly don't think it would take more than a day to understand everything TS offers.

Re: JavaScript libraries should be written in TypeScript

#119

Earlier quoted context omitted.

With `--target=es6` I'm not sure if the TypeScript compiler does any code transformation other than removing type annotations. So the chance of a 'transpiler error' is basically zero.

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.

Re: JavaScript libraries should be written in TypeScript

#120

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!

Simple solution - offer the compiled file in the library. Everyone wins.

This is the default way of doing things on npm that I've seen.

http://stackoverflow.com/questions/29738381/how-to-publish-a...

Post reply on HN