112 points · by smokel · 13y ago · 89 comments
The language itself seems like a winner as well. Its conservative design makes it the perennial dark horse of the compiled-to-JS language family, but that's also what makes it most attractive to me. It's a thin-enough skin over the underlying language that you get the familiar Javascript syntax (as per Dart) while also retaining the ability to understand the generated code (as per Coffeescript). It's also got well-defined corporate backing, which in a professional environment gives me more peace of mind than recommending Coffeescript (does jashkenas get paid to do this?), Clojurescript, or Elm (I'm somewhat concerned about Dart here as well, at least until Google starts using it in production). These properties mean that I'd happily recommend TypeScript for client-side enterprise software development (but only once the compiler hits 1.0, of course).
With that said, as ES6 evolves and becomes more widely supported there will be aspects of the TypeScript compiler that will no longer be needed (e.g. classes, modules, fat arrow functions, etc.).
I think that is a valid statement only within the context of the next ECMA release. In other words, that's not to say that whichever successive version after ES6 can't include it, or any version thereafter.
[1]: http://wiki.ecmascript.org/doku.php?id=strawman:guards [2]: http://wiki.ecmascript.org/doku.php?id=strawman:trademarks
I really hope I can do something like
for (
ra <- asycn_opA(),
rb <- async_opB(ra),
_ <- asycn_opC(ra, rb)
).run() spawn(function*() {
var ra = yield async_opA();
var rb = yield async_opB(ra);
yield async_opC(ra, rb);
});
This works right now in Gecko: http://taskjs.org/. Something very close ought work in ES6/Harmony. Q.async(function*() {
var ra = yield opA();
var rb = yield opB(ra);
yield opC(ra, rb);
})().done();
Are you wishing for the other syntax or something that you can't achieve with generators/promises today?Here's your example rewritten in the 'monads' library (I probably need a better name):
monads(function() { var ra = opA.defer(); var rb = opB.defer(); var result = opC(ra(), rb()); });
It is good to see people try different things though, so congrats on release.
In practice this has meant that code that doesn't change much but is called frequently is fully typed, while code that's under more active development is less typed, unless it's tricky.
I've only just started playing with the noImplicitAny setting, where the compiler complains loudly about any identifier that it can't figure out a type for. Not sure what I think of it yet. I'd kinda like something that lets me do a targeted noImplicitAny, but it might be enough to just do it at the file level (i.e. these files should be fully typed, these files shouldn't).
From my experience with Dart, I'd say it can work really well. Basically, if you just put type annotations at the API boundaries, you already get most of the type related benefits you would get with something like Java or C#.
Inside your functions, you can generally omit types. For the tools, there is enough type information floating around to make sense of everything.
If you use a literal, the type is known. If you use some annotated function, the type of the return value will be known. If you assign any of these things to "var", the actual type will be remembered. This means you get sanity checks, call-tips, and auto-complete without having to annotate the types there.
Putting types at the API boundaries also acts as documentation. It's so much better than writing JSDoc comments. Plus, you get all the benefits right away. It's a very sweet deal.
You are probably going to end up using types a lot if you are using projects that already have type declaration files available (from the DefinitelyTyped project).
Well, there is a downside in that you're now having to choose between adding type annotations where you don't particularly want them or ignoring compiler errors (warnings?). The former puts types everywhere, the latter partially defeats the purpose of static typing and adds "sort out the errors I care about" as another work duty.
My understanding is that with typescript you can use .js/.ts files as a relatively coarse mechanism of segregating out your untyped code, but it isn't entirely roses.
That is just not the case. Types get inferred. If the inference isn't making it all the way through that means you have dynamic types, not a compilation error.
And yes, ignoring warnings is crazy. TypeScript has very few warnings, mostly just errors that can't be ignored.
False dichotomy.
You want both and you need both.
Why? Let the compiler do the testing for you.
I love the idea of optional typing being supported (whether in the base language or TypeScript/Closure/etc) but it's maddening to think of trying to have to add the type information manually to existing code, especially code with lots of good test coverage, knowing that the JavaScript runtime seems to have to figure it out anyway (at least based on talks about V8 from the past).
This kind of functionality seems even more important for projects like DefinitelyTyped, where people are trying to track the public API's for projects that sometimes have a lot of churn. Having runtimes export their runtime type information (which seems to have sufficient mapping to source based on existing debugger experiences in Chrome, IE10+, etc) and allowing the creation of tooling that can populate/check/etc the type information in source seems like a big potential win.
Anders asking the Chakra team for this to (massively, AFAICT) help TypeScript adoption among existing projects seems reasonable, but TypeScript type information seems to still be 'add it yourself' for now?
Am I just missing something that makes this far more complex/difficult/intractable than I realize?
However, I think if someone came up with a system to add optional typing through the use of string literals it would be awesome. That way we can have perfectly valid JavaScript with the benefits of certain JavaScript engines and type checkers. The string literal would be before a function declaration like:
"add(Number, Number) -> Number";
var add = function(x, y) { return x + y; };
That would basically specify that the function takes two numbers, and returns a number. This system would not replace a language like TypeScript, as TypeScript provides much more functionality. But it would allow for really simple optional typing and for TypeScript to take advantage of this(in the case that a JavaScript engine eventually implements and optimizes for these type declarations).The Closure Compiler has a whole type system that lives in doc comments:
https://developers.google.com/closure/compiler/docs/js-for-c...
and I believe there's a command line option for doing type checking. Alas, Closure Compiler is written in Java rather than JavaScript so you can't use it quite as seamlessly as TypeScript's compiler.
I also hope that they make a better module system.. currently it's pretty confusing and I don't really know how to differentiate between TypeScript and Node.js modules.
Reading through the marketing material from MS it seems like the entire value proposition is "statically typed languages make it easier to think about your program, also we added the class keyword so you don't feel lost"
I suppose if you believe those claims or are really interested in using MS development tools, TypeScript is for you.
IMO, ClojureScript is much more interesting and has a far greater value proposition for complex front-end development.
I believe EcmaScript 6 plans on adding the class keyword and TypeScript's implementation reflects the current draft of how classes in ES6 will work.
> ...statically typed languages make it easier to think about your program...
Out of curiosity, do you normally program using statically typed languages?
If not, I can see why you would be underwhelmed. Then again, I shouldn't be surprised by your reaction. This is Hacker News and TypeScript is by Microsoft.
I do not use statically typed languages and I have nothing against MSFT; I was solely commenting on TypeScript as a language.
TypeScript's best feature (imo) is its (optional) static-typing so if you don't like static-typing to begin with, then yeah, it doesn't have much else to offer you.
To each his own.
It does help Visual Studio and other tooling, but to say it is only "to sell Visual Studio, nothing more" is ridiculous.
The more likely conclusion is Microsoft is open-sourcing a tool the found useful internally when building large JavaScript apps.
I'll have you know I paid for my WebStorm license because of its TypeScript support =)
From what I understood, they both basically solve the same problems, one just does it in a Ruby-esque style while the other does it in a C#-esque style.
I've been using TypeScript in my startup the last 6 months.
They are definitely not the same. TypeScript is JavaScript + types, whereas CoffeeScript is a new language, a mashup of Ruby and JS. (For example, you can take any JS on the web, paste it in a TypeScript file, and it will compile. But you absolutely cannot do this in CoffeeScript.)
CoffeeScript tries to do clever things for you, which often results in inefficient code. For example, say you've got a function that ends with a for loop. CoffeeScript will actually convert that into an array allocation and return the result of the array. (Looping over 1000 elements at the end of a function? CS just allocated a new array which will be dynamically resized at runtime multiple times, allocated 1000 buckets for the elements.)
CoffeeScript also can't make up its mind about whether parens are necessary. Function with multiple params? Not necessary. Chained function, like jQuery? Necessary. Most of the time. Function with no params? Necessary, unless you use the 'do' syntax.
It's a mess.
TypeScript, on the other hand, is designed by respected, experienced language designer Anders Heijlsberg. His language wisdom and overall vision for the language shines through.
Bottom line: Both are better than vanilla JS, I'd say. But I work with CoffeeScript when I have to. I work with TypeScript because I choose to.
Simple, just return anything else, or nothing at all:
myFn = () ->
for el of array
# loop body
return
> CoffeeScript also can't make up its mind about whether parens are necessary.Well, in some cases they're optional, but it's predictable and hardly "can't make up its mind". I've never run into a problem with this. Implicit parens wrap to the end of a line or block expression, and it feels pretty natural:
f g h obj -> f(g(h(obj)))
f g, h, obj -> f(g, h, obj)
f().g().h obj -> f().g().h(obj)
Every time there's a space, you're introducing a new fn call. Every time there's a comma, it's adding a parameter to the fn call. So yeah, you will have to use parens for any chaining style (a la jQ and many others), and to disambiguate complex nested fn calls, but that's expected.I prefer to think of parens as sometimes optional.
Thanks for the thorough info though, I've only used TS a little and CS none at all, though from what I've seen I'd side with you in that CS tries to be too clever and/or is too foreign for my non-Ruby brain, whereas TS more closely melds with my skillset.
> For example, you can take any JS on the web, paste it in a TypeScript file, and it will compile.
This myth won't die.
False. Typescript will choke on most JavaScript files from the web. Because you will need to include definition files for any external library calls. If you use jQuery, for example, the compiler won't like your $('.class').doStuff() calls because it won't recognize it until you go find the jquery.d.ts file to include at the top of your file. Also, TypeScript will struggle with the dynamic-ness of JavaScript. To prove this to yourself, paste the contents of typeface.js into the play compiler at www.typescriptlang.org/Playground/ and you'll see all the red squigglies that you have to fix.
On the other hand, if you can do it in JavaScript, you can do it in CoffeeScript, because CS is NOT statically typed. You can use js2coffee to convert any valid JS on the web to coffeescript.
In my testing it takes me 25 times longer to take legacy code and convert it to TypeScript than to convert it to CoffeeScript. That's because "valid JavaScript is NOT valid TypeScript."
> CoffeeScript tries to do clever things for you, which often results in inefficient code.
I find that CoffeeScript's results are BETTER than the TypeScript code. For example, CoffeeScript automatically does array length caching in it's for loops, resulting in much faster loops. It also correctly converts == to === in comparisons, eliminating many nasty JavaScript bugs. TypeScript does neither. In my analysis, TypeScript generates poorer code than CoffeeScript.
> CoffeeScript also can't make up its mind about whether parens are necessary.
Use them all the time if you want, CS isn't dogmatic about it. EcmaScript 6 (and TypeScript too) will be moving toward optional parentheses too. It's a nice feature.
> TypeScript, on the other hand, is designed by respected, experienced language designer Anders Heijlsberg. His language wisdom and overall vision for the language shines through.
Anders has done a fine job of making TypeScript look like C#. It's one redeeming trait is that it helps the tooling for allowing refactoring and intellisense. I find it cumbersome and you have to put a lot of ceremony into your code just to get it working. It is roughly 35% more lines of code to do the same thing in TypeScript that you could do in CoffeeScript, and you are fighting the compiler more often. I think TypeScript would be an incredible burden for enterprise applications because of the need to include definition files. Think of the joys of upgrading your version of jQuery and having to find the jquery.d.ts file to go with it.
I recently presented a talk at a conference on the two languages and after evaluating both languages in depth, CoffeeScript was the clear winner. Both in the code it produces, but also the amount of effort to develop in it.
"CoffeeScript is only syntactic sugar. It brings very little new to the table, but perhaps worse of all, it doesn't really fix all of JavaScript's WTF-issues while adding a few of its own. While it's an improvement over JavaScript, its advantages are sometimes outweighed by the extra hassle to compile and deploy it"
Just my observation.
TS (or Dart) makes JS more toolable and thus more scalable. CS, on the other hand, just offers a keystroke reduction by dropping the "function" keyword, semicolons, and other punctuation.
TS and Dart also reduce the number of keystrokes though. They allow you to auto-complete pretty much everything.
Reducing boilerplate for loop code:
CS:
for el, index in arr
el.doThing(index)
JS:
for(var index=0; index < arr.length; index++) {
var el = arr[index];
el.doThing(index);
}
Better iteration over objects: CS:
for own k, v of obj
console.log "#{k} is #{v}"
JS:
for(k in obj) {
if(!obj.hasOwnProperty(k))
continue;
console.log(k + " is " + obj[k]);
}
Default function params: CS:
foobar = (foo = "default", bar = "shmefault") ->
# Go on...
JS:
function foobar(foo,bar) {
if(foo === null)
foo = "default"
if(bar === null)
bar = "shmefault";
// Go on...
}
String interpolation: CS:
str = "There are #{num} #{item}s available for $#{price} each"
JS:
str = "There are " + num + " " + item + "s available for $" + price + " each";
Existential checks, soaking nulls: CS:
if obj?.prop?.exists?
console.log "obj.prop.exists"
JS:
if(typeof obj !== "undefined" && obj != null && obj.prop != null && obj.prop.exists != null)
console.log("obj.prop.exists")