Announcing TypeScript 0.9.1
blogs.msdn.com
blogs.msdn.com
I'm a big fan of TypeScript's approach, which gives you the best of both worlds. Start with fully dynamic code to explore the problem space, then add more static guarantees as you firm up your design.
I haven't had occasion to write a big web app recently, but I'm itching to find one so I can put TypeScript through its paces.
Don't everybody mob him at once :-)
1. Static type compiler verification saves tonnes of problems. Try managing a 2MLOC dynamically typed solution and you'll get what I mean.
2. Static typed languages are way easier to refactor as more metadata is available to the tooling.
3. Static typed code is easier to test. The type contract is available over the boundary between the implementation and the test cases.
4. Defined interfaces without leaky abstractions are easier to produce when you have static types.
5. Static types allow the compiler to infer more information about how to compiler your code resulting in faster, more efficient code and lower memory usage plus you don't have to compile an instance of a function with every possible type consideration at runtime.
6. Statically typed data serialises more reliably i.e. goes over the wire easier without behemoth contracts and parsers at each end.
7. Statically typed languages tend to have better numeric accuracy as differences between decimal, floats and integers are always deterministic and there are defined casts and conversions between each during operations.
I could go on...
C is hardly a good representative for an statically typed language. Read Luca Cardelli's "Typeful Programming"[1] to get an overview of what typed programming is all about, and see for yourself how brain-dead C and C++ are in comparison. After that go to Benjamin Pierce's canon, "Types and Programming Languages".
It introduced stronger array types, eliminated Cs automatic void* -> T* conversion and, most importantly, put higher-order functions and types on the table.
It might be a warty syntactical abomination, but it can still hold its own against some of the languages of type extremism.
In general, C's type system is so weak as to be both useless and a hindrance.
6 you're adding metadata with type definitions. Enough metadata is present in entirely statically typed languages to not do this.
7. How do you know if 1.000000000000000000001 is decimal or float?
It's weak in some places but strong in others. Knowing when to use each case effectively is the art.
7. It's a float, unless you made it a Decimal explicitly. Arguably that's the wrong default, but almost no languages default to arbitrary precision/size everything.
No, C's type system is totally unsound. It fails to catch very bad errors (pointer-related issues, null-related issues, etc.) and is perfectly happy to implicitly cast left and right. It's little more than a hindrance.
C has some static type safety, and virtually no run-time type safety. Java on the other hand, has a lot of static type safety, is completely memory safe (barring bugs in the implementation), and checks all coercions dynamically. Python has what Java has dynamically, and nothing statically, but it is still 10 times better than what C has!
Java don't forget suffers as do other languages from lots of nasty things related to types including invalid casts, null reference exceptions etc. When these go phut in production, you're usually in the same situation.
Options, like all collections, can be chained together as such:
for {
x <- a(1)
y <- b(x)
z <- c(y)
} yield z
this expression is of type Option[Int], and will never throw a null pointer exception. (Futures can be chained together with identical syntax)But match exceptions do.
Anyways, if I had to choose between unsound static type checking and sound dynamic type checking, I would definitely choose the latter.
I'd call that non-sense.
You have a typed pointer to a function but that's not a void pointer and the "New C Standard" 6.3.2.3 states that you can't cast a function pointer to a void*.
int sqlite3_exec(
sqlite3*, /* An open database */
const char *sql, /* SQL to be evaluated */
int (*callback)(void*,int,char**,char**), /* Callback function */
void *, /* 1st argument to callback */
char **errmsg /* Error msg written here */
);
Here the void * is an argument that is passed in the callback as well so that you can update your own struct with the result, for example. Singe the library cannot know anything about your types, void * is the only option."What's true of all bugs? They passed a type checker and they passed the tests!" - Rich Hickey
Does it catch everything? Can you stop thinking critically? No, but it's still nice to have.
This sort of goes hand in hand with IDE tools (e.g. "change method name" sorts of things), so I don't think we necessarily disagree.
> I've never seen a bug in production that could have been prevented with static analysis.
That seems unlikely to me.
I do application penetration testing for a living, and I'll often use static analysis tools in my work. (Both robust, established tools and ad hoc scripts.) For example, check out Brakeman for Rails apps. These tools find actual bugs in actual production software.
Do they find everything? No, you can't rely on them completely. But they're still nice to have.
I know that there are definition files for it [1] (and many other popular libraries), but unfortunately I haven't had the time to check out their quality.
[1] https://github.com/borisyankov/DefinitelyTyped/tree/master/a...
The scoping with modules and the typing really helped since I'm familiar with C# and having "everything light up" in the IDE. Saved a ton of time letting a compiler catch things instead of writing tests.
I tried coffee script, but found the copy/paste option for javascript that I have already written won me over.
No longer am I concerned about errant semi-colons or a stupid typo in a method name on an error handling path that a unit test missed.
And when valid javascript is valid typescript, it seems odd to not opt to use it when the only addition is a compile step to ensure sanity.
We found a bunch of real and potential bugs due to JavaScript's dynamic nature, particularly with regards to the number of function arguments (JavaScript being happy to ignore extra parameters or replace missing parameters with undefined).
Saved a ton of time letting a compiler catch things instead of writing tests.
Types should never mean writing less tests :/type annotation tests that at compile time
There might be some worth in checking the structure of an object (including attached functions) as it leaves a public api, especially in a language like Javascript that plays it so fast and loose with object construction.
That's one of the nice thing about typescript - you can use it where it's most helpful and leave it where it's not.
Type checking, or static analysis in general, checks for the presence of a particular class of errors.
Unit tests check for correct behaviour, and in doing so imply a lack of any errors, types or otherwise.
While it can be good to use unit tests to check for regressions in a specific error, the core of your tests should be specifying the behaviour of your system, not trying to catch out any specific class of errors.
If your tests are busy looking for specific errors, there will always be some that you miss (type checking, even in Haskell, misses a lot of classes of error). If your tests check for correct behaviour, there can be no errors.
Of course, writing tests with perfect coverage and specifying all the behaviour is impossible, even more so when you've got actual features to get out the door. So any static analysis you're comfortable with can be great for making up some of the slack, but let's not pretend that in most cases we shouldn't now need to be writing those tests to check it does what it's meant to.
The place where this approach sometimes falls short is on the public API boundary, as I mentioned in my other post. Types can be useful there, so maybe my original assertion was a little too strong, but still types should very rarely mean less tests.
(Apologies if the post is a bit all over the place, redrafting on a phone is hard)
especially for testing return types as C for example you assign those.
In C it's more popular to write example code which uses the library, and the errors are spotted at compile time.
http://blogs.msdn.com/b/bharry/archive/2012/10/24/typescript...
I developed the first prototype in pure JS and there were some bugs (due to me being new to this FireBase model, and some hastily developed code).
I then converted the whole thing to TypeScript, because I prefer static typing. It took some time to setup: upgrading to the latest nodejs, collecting all type definitions for popular libraries, etc. The DefinitelyTyped[1] library is a great resource. But there were some warts in it. I had to make some PRs to this library for some missing methods in jQuery.
But right after finishing the setup, I was able to find and fix a complicated sequencing bug. That alone was worth the trouble of the setup cost.
I then tried using TypeScript for another project (this one had a licensed IDEA available). Here the setup cost was even lower. There are plugins available for Typescript in IDEA. Though I couldn't convince everyone in the team to switch to TypeScript.
This was back before they added generics support. Now that generics is added, I think there is hardly any excuse left to not use TypeScript (or a similar alternative).
What would make TypeScript awesome is support from more IDEs (Eclipse especially).
There exists a plugin for eclipse, but I have never tried it. I'm mostly an Intellij IDEA user.
A real aid to productivity in my opinion. And I love how you can turn up and down the typing as you see fit.
The compiler speed is OK (well, 0.9.0 wasn't, but 0.9.1 should be back to where it was before.) The main pain point is that we usually want to type any 3rd party JS we interface with (e.g. Angular, Backbone, Highcharts etc.) though https://github.com/borisyankov/DefinitelyTyped is helpful with this. No major complaints as of yet.
It's still perhaps too early to tell how long MS will be supporting it. Windows 8 has a JS interface which is comforting, but they could turn around and decide to stop development on it. That would be a great shame, but the code is open source, the compiler works well as of today, and the generated JS is readable enough that it can be edited.
We're slowly migrating over from Coffeescript.
The optional typing ability is a life saver when your code base grows. We've used the integrated tooling with Visual Studio which also helped a lot.
Microsoft is promising it will be kept in sync with ECMAScript 6, so we don't really think there is much risk in using it. It's definitely helped with productivity, and the learning curve isn't high for JavaScript developers.
The benefits of inferred typing is great. Mostly you just need to explicitly declare the types of your class properties, and then code that uses the properties or modifies them (like .map() or .forEach) doesn't need explicit types defined - so it still looks like JavaScript.
Refactoring is no longer a search and replace in files (and pray tests pass) affair.
Dealing with other programmer's code is no longer as mentally taxing.
It's little things like this that add up.
It is completely doable to simply let the external libs call to be dynamic and guard your core code with static typing.
https://github.com/google/traceur-compiler
however its development kinda stopped in last months
Often the stuff that "just works" doesn't end up producing a lot of chatter on the intertubes, at least not nearly as much as something that looks seductively simple but turns out to be much harder in practice.
Another example I like is Lua: elegant, mature, and used in some very very big applications. If there was ever evidence that a codebase was high quality and nearly bug free, it's that. But when was the last time we had a good flamewar (with namecalling) over Lua?
EDIT: to add, I like the idea of typescript (it's a much smaller step than compared to something like Dart). But their tooling is all around Visual Studio, which ties you to Windows. I don't dislike Windows, but I want my development tools to be cross platform (which is why I like Sublime Text, Eclipse, and similar tools).
npm install -g typescript
Still not sure why you're complaining. There's great support for TypeScript for Sublime, Vim, Emacs, Intellij IDEA and Eclipse.
http://msopentech.com/blog/2012/10/01/sublime-text-vi-emacs-...
http://blog.jetbrains.com/webide/2013/02/typescript-support-...
There are definitely attempts to bring the language service to other editors but when I tried them last (just before the 0.9 release) they seemed either featureless or very slow. If someone can recommend one for sublime/emacs/vim that they actually use I'd be very appreciative!
EDIT: related thing that annoyed me: the language service is part of the TypeScript repo but as far as I can see from there is no official documentation on how to go about leveraging it for other editors! I admit I can kind of understand this until the language is less of a moving target. The best way to get started for now seems to be looking at third party efforts like https://github.com/clausreinke/typescript-tools.
It's a little disingenuous to expect environments that are typically more text editor than IDE and much more lightweight (Sublime, Vim, Emacs) to have all the features of an IDE with as new as TypeScript is. Much of those features require much more work than they would to implement in Intellij or Visual Studio, which have great APIs for building plugins with those sort of features[1] even for the community[2].
[1] http://confluence.jetbrains.com/display/IntelliJIDEA/Custom+...
[2] http://plugins.jetbrains.com/plugin?pluginId=5055 (The third party Lua plugin for Intellij for example)
I don't doubt that it's more work to support more than syntax highlighting, but that doesn't change the fact that the support in those editors is sub-par when compared with lots of other languages. For example I can install SublimeLinter and have basic error reporting for python code, or SublimeJEDI and have code completion and go to definition.
To answer your question: Yes! TypeScript is quite portable indeed. I have run it with Visual Studio, I have run it with Node.js, and even a JScript interpreter.
I hope to be able to use TypeScript for years to come, it solves the problem of opt-in types in JavaScript.
"...you can develop TypeScript code using the online playground tool or Visual Studio 2012. But this is not it! You can also use Sublime Text, Vim or eMacs as the team has kicked off work on syntax files for these popular editors JavaScript developers love to use. And as the specification is public, anyone can create their own syntax files for other editors as well."
http://msopentech.com/blog/2012/10/01/sublime-text-vi-emacs-...
Sometimes the compiler issues an error it is really hard to get what's wrong without visual clues , unless one likes to count lines and columns and switch between command line / text editor all the time.
Does the TS compiler even work with Syntastic(vim) by the way ?
I like TS , i use modules , classes and short hand functions declarations , but not the type system which doesnt make sense to me. It doesnt make sense with javascript and force back devs in all these java EE like patterns.
Anders talks about that problem in this video: http://channel9.msdn.com/Events/Build/2013/9-006
edit: just the points