Announcing TypeScript 3.0
blogs.msdn.microsoft.com
blogs.msdn.microsoft.com
I really hope they continue doing so and it's not just an attempt to take over a market then lock it down how they want. What's the term? Embrace, take over, discontinue?
Coffeescript was an entirely separate language that just happens to compile to Javascript. The whole fiasco with Coffeescript 1 -> 2 was that 1 only compiled to ES5 and couldn't support ES6+. Migrating to Coffeescript 2 is basically the same amount of work as migrating to modern day Javascript. You can't even write Javascript within a Coffeecript file without it breaking.
Typescript IS Javascript. You can copy paste code both ways and they'll still work.
How do you figure that? As far as I am aware that honor goes to the CoffeeScript project, whether you hate it or love it, it's been the main inspirator for JavaScripts advances. And second to CoffeeScript would be all the little libraries that inspired JS's standard library expansions like JQuery and Underscore.
You're right about jQuery and underscore, though I think jQuery's are often overstated (it and Sizzle inspired the querySelector API in browsers' DOM implementations, and not much else, underscore has had wide-ranging implications for ES6 in general)
But more importantly, the whole idea of ES6 was inspired by CoffeeScript. This might seem weird if you were outside the loop at the time and don't realize how succesful it was, but the fact that ES6 got so many cool features was directly due to CoffeeScript's success.
If ES6 wouldn't have those features, people would not have switched from CoffeeScript back to pure Javascript. And the success of Typescript shows that they still have to move quickly to keep pure Javascript relevant.
Here's a list of comparisons between CoffeeScript and ES6/7:
https://github.com/hemanth/coffeescript-equivalents-in-es6
Note that these are features that were not in ES5, then they were in CoffeeScript, and only then were they added to ES6. The similarities are in no way a coincidence, almost all of CoffeeScript was in one way or another integrated into ES6.
"Arrow functions" (lambdas) existed long before CoffeeScript. Arrows of varying types (usually => or ->) have been standard syntax for lambdas for easily decades. One of the language designers on the TS team was involved in a language that had lambdas a year before CoffeeScript came into existence. To call lambdas a CoffeeScript innovation is like calling actors an Elixir innovation.
On the subject of CoffeeScript inspiring ES, that is likely very true - at the very least it lit a fire under an extremely stale language. They carefully selected some very good ideas that worked well together and made a beautiful language with them. However impressive, beautiful language design is not innovation. Something like optional any is.
CoffeeScript had this mix of language features I think any language aficionado could have picked up at the time. It was just crazily succesful, no doubt due to its flawless execution by Jeremy Ashkenas.
I think the question was about whether they existed in the JavaScript community.
I'm sure much of Typescripts great features are not completely novel nor unique—e.g. generics come from c# and Java—but it is responsible for bringing them to JavaScript.
Not sure what "outside the loop" means here but do you have a source for this statement? I followed ES proposals quite closely throughout and wasn't aware of any such thing.
> Here's a list of comparisons between CoffeeScript and ES6/7:
> Note that these are features that were not in ES5, then they were in CoffeeScript, and only then were they added to ES6
Unless you supplied the wrong link, I think you're mistaken here. Of the many examples in the link, the majority are code that was in ES3, or—in the case of array methods—bare little resemblance to Coffescript (I think a lot of those were inspired by Underscore).
The exceptions are fat arrows and spread, and I think the latter has a more complex history.
Two other notables in that link: lexical scope was in JavaScript 1.7, introduced in Firefox 3.6, predating Coffescript's introduction. Generators had an early form in JavaScript 1.8 but the syntax changed for es6, and Coffescript adopted it after es6.
Here's a nice article showing that CoffeeScript was on Brendan's mind when he was working on Harmony (ES6): https://brendaneich.com/2010/11/paren-free/
He calls Ruby/Python/CoffeeScript "unsyntax" and he doesn't like it I think :P
edit: apparently he does like it, he just doesn't think it would work on the web, he elaborates here: http://intertwingly.net/blog/2010/11/25/Hobgoblin-of-Little-...
The page is a list of examples of Coffescript done in Javascript. Whether they show evidence of Javascript taking features from Coffescript isn't really a subjective question; they do or they don't. I explicitly enumerated which do and which don't.
In terms of "experiencing the times differently", that is subjective, so I guess we did. One blog post from Eich on why he doesn't want to remove braces isn't really holistic, but you might be right in general.
I'm not sure how you figure you enumerated which do and which don't. Apparently I'm preaching something unpopular, all my comments have been downvoted a bunch.
And while I'd like to agree with you that having types in Javascript is more important than the coding comfort CoffeeScript brings, I still think ES6 was more important than Typescript is or will be. Before CoffeeScript coding in Javascript was at best annoying, and at worst absolutely terrible.
e.g. I've got data like this:
struct Point{
float x, y, z;
unsigned char r, g, b;
unsigned short intensity;
double gps_time;
}
Point[] points = new Point[1234];
Reading, generating, and manipulating data like this is a bitch in javascript. Typed arrays have to be aligned to the byte size of it's type and because the offset of gps_time isn't at a multiple of 8, it's not possible to read it with Float64Arrays. DataView works but it's an order of magnitude (!!) slower than typed arrays so the best thing I can do for now is to read a double by treating everything as Uint8Array(), and then read the 8 bytes of the double into a temporary ArrayBuffer that can be used to convert between types.My hope is that Typescripts type system is good enough to be able to treat this kind of data similarely as you can in C++, and that it would then become standard.
let x: number = "some string" as any;This actually happens a lot with types from DefinitelyTyped that are not maintained by the original author of the library.
Curious, which languages compile that fast to WebAssembly?
And well over 2 seconds for Webpack to deal with (just) bundling 6 JS files and the .wasm file.
It would be nice to have some more targeted solution for working with typed arrays, though.
Microsoft has invested quite heavily into some very large TS codebases: VS Code/Monaco, VSTS, the Azure portal, large components of Office, large components of Bing, and so forth. Given the scale of some of those projects (in terms of users at the very least, even before you get into metrics like lines of code or dollars generated), that is a lot of benefit to having a strong tool like Typescript to do that work in. Many of the same developers working in Typescript use C++ and/or C# daily, so they already understood the usefulness of things like static type checking. Releasing it to the public when they did seemed a very cogent realization that that "enterprise JS application problem" was far bigger than just Microsoft and they were in a good place to help with that, given it's long been a goal of the company to help developers.
As for a Typescript proposal for TC39, I'm willing to bet everyone is still worried about accidentally creating a second "ES4" debacle, including Microsoft. I'd love to see, and would much more expect at this point, a pragmatic compromise like Python's PEP 526 [0] / PEP 3107 [1] project, encouraging JS engines to syntactically accept things like Typescript's type annotations, but remaining agnostic to what the annotations represent semantically (giving other JS users the option of doing things with the annotations other than types, too, if they wished).
[0] https://www.python.org/dev/peps/pep-0526/ [1] https://www.python.org/dev/peps/pep-3107/
So sure it is likely that in many cases browsers will still mostly see JS with stripped annotations, but the reason to do it is the same as the debate over whether or not to strip comments and/or support sourcemaps. There's clear wins at the very least in debugging situations to allow unstripped type annotations all the way through to the dev tools. There are even cases where that may make sense even in a production environment, including things like metaprogramming if those annotations are reflectable.
This nice thing would be to have the option, whether or not most projects might still prefer to strip them out to optimize bundle sizes. As with almost all things, it's an interesting spectrum of tradeoffs.
Are you really sure?
To me that attitude sounds awfully close to cargo-culting.
TypeScript makes JavaScript easier & less buggy IMO. The more people developing with TypeScript, the better chance they use Microsoft's other tools. Some of those tools cost money. Some also make deploying with Azure really easy & enjoyable. Azure is their big cash cow.
The more MS tools you use & the better perception of the company you have, the easier it is to find yourself just wanting to stay in their ecosystem.
If you do Kubernetes type deployments they have some nice offerings for all levels of knowledge. They just released Service Fabric Mesh for those who know less about it.
If you just want a simple deployment, their App Services is really slick & easy with Github integration for JS, PHP, .NET & other stacks.
They also have some really good integrations with their serverless stuff that decreases the amount of code you have to write significantly.
I actually only know web developers using Macs to edit their TS, maybe a rare case of Linux, zero other MS things used, except maybe VS Code. And maybe it's debatable if VS Code is the best TS editor, I also have no numbers about widespread use, but I doubt it's a majority.
It started off taking language queues and inspirations from C# and other languages and is now at the point where they are the ones copying back. Impressive stuff, and it’s really come a long way since 1.0 which was so much more awkward to use.
I've never liked front-end development, but my first Typescript project was something of a revelation for me - a familiar syntax, static types, async/await, automatic fields via some ctor sugar, and a great type system! I really like the way it blends aspects of C# with JavaScript and F# - if you didn't known it had Hejlsberg at the helm, you could easily guess.
I’m hoping the eventual way that typescript style typing helps with perf is by presenting new compilation modes that make it possible to detect at runtime places where the code executed (at runtime) might not be providing the performance that could be expected by the types (because of a lack of runtime deoptimization or lack of ability for runtime to specialize in the circumstances presented during the program’s execution).
I think that could be a pretty powerful way forward — and very pragmatic given that the vast majority of code in any code base doesn’t need to be particularly fast ... I think what’s needed in this domain are (1) powerful tools to help maintain correctness (2) a smooth way to find and fix performance issues where they occur — and ideally by being able to write those fixes in the same language. I think those more useful features for a web scale language than a language that bends over backward in terms of expressivity to make “everything fast” ...
But what would they call it?!
;)
I have to say though that I like Typescript so much, precisely because it has such a great type system.
Not only that but once you have types for inputs, it can infer a lot of things. Augment the humans with almost magical code completion ability because it can reason using types and static code. It helps catch a whole bunch of issues in a dynamic language like Javascript.
I’d say Typescript makes javascript fun and scalable to use.
I've been using Flow for so long now. The techno is good, the integration with the code editors is good too, but Flow has a lot of small-but-really-annoying bugs (2235 issues on GitHub right now), the type definitions for third-party libs are meh (you often have to update/fix the definitions by yourself), the team seems extraordinarily unstaffed for such an important project, they don't share the roadmap at all, the priorities are clearly internal first (like improving the performances instead of fixing the bugs) and they don't seem to do much marketing or communication about it.
I wonder what's the future of Flow at this point.
And I'm seriously thinking to move to TS (for pro and personal projects) once create-react-app will work perfectly out-of-the-box with TS.
And re: create-react-app, have you tried react-scripts-ts? I've been using it all year with no real issues.
Turns out there are none. You have two compiler flags: Target and Lib. Lib determines the syntax and objects/apis you can use: ES2015, 2016, '17, etc, and Target determines which platform it will run on.
But as far as I can tell, setting target does nothing to materially change the output code. It doesn't polyfill. Rather it just causes additional configurations to feed into the Lib compiler argument.
How to Polyfill with Typescript: https://basarat.gitbooks.io/typescript/content/docs/types/li...
More on Target: https://basarat.gitbooks.io/typescript/content/docs/types/li...
1. Goto http://www.typescriptlang.org/play/
2. Enter the code `function* gen() { yield "foo"; }`
3. Behold the mighty state machine in the compiled output.
Have a look at the Babel website. There are tons of plugins and features: generators, symbols, async generators , object spread (long before TS had it), .........
Then there is automatic tracking of what is supported by your runtime , e.g. you can say “use polyfills for anything not supported by the current version of node”.
.babelrc is a complex beast, because polyfilling is complex. Let Babel solve that, and focus your TS energy on typechecking.
Yes, it gives some compile-time-only type safety in certain places, but it doesn't provide state safety like ClojureScript. I'm more worried about state/reference-based bugs creeping into my code versus type related issues, which I'll be checking via unit tests anyway. On top of that, it doesn't protect you from the usual JavaScript pitfalls since it's backwards compatible, including the bad parts.
Or am I missing something here? Is it popular because it lets you directly upgrade existing JavaScript files? I can see that being a big draw. Small and gradual improvement, yeah?
For example, it couldn't mean atoms/CAS because it's already single-threaded.
Also, you'd consider upgrading to Typescript because static-typing was on your wishlist. ClojureScript isn't really a competitor.
In fact, I ultimately found that "I still want a dynamically-typed language but with some technical improvements" is the weakest reasoning for switching away from Javascript which is why I ended up dropping ClojureScript. Meanwhile I've found something like Elm goes far enough to make the switch more worthwhile (static typing, built-in front-end architecture, etc).
How would you recommend communicating all that in an extended version of ECMAscript?
If you have a function from A to B, you have to declare its type as
type MyFunction = (arg1: A) => B
instead of type MyFunction = A => B
Here's that syntax applied to the example from GP: function call<TS extends any[], R>(fn: (...args: TS) => R, ...args: TS): R { return fn(...args); }
vs. function call<TS extends any[], R>(fn: ...TS => R, ...args: TS): R { return fn(...args); }This construct would actually be more complex if the parameter list types were actually specified instead of being any[].
I call code like this 'clever' as it uses the language constructs to get usually a very optimal end result at the behest of being harder for n00bs like myself to parse.
I do wonder what a prettier version of this would look like. It certainly wouldn't be this concise but would it be better or harder to follow? I'm okay with something like this if it solves a very specific problem I hardly see. If my entire code base is littered with this? No thanks. Yea eventually it'd be second nature but I'd rather it be more verbose and slightly easier to unpack than this tightly packed.
Secondly, the (...args: TS) => R can be extracted into another type/interface, this way you would just get "function call<TS extends any[], R>(fn: Callable<TS, R>, ...args: TS): R"
“Big JavaScript codebases tend to become read-only.” ~ Anders Hejlsberg
Anders Hejlsberg and Lars Bak: TypeScript, JavaScript, and Dart https://channel9.msdn.com/Shows/Going+Deep/Anders-Hejlsberg-...
Often we find that we create a nice model in TS, only to handle a lot of switching on types in the runtime. This is sometimes a smell but sometimes unavoidable, unless type information can be present in the runtime.
All the TypeScript dependency injection libraries rely on decorators or something else requiring modifying the classes being injected.
It makes the code look terrible and this is really my only complaint about TypeScript which is otherwise fantastic. I basically only do DI by hand because of it.
Also, unknown is a great addition.
EDIT: Just checked out their roadmap and while not exactly what I had in mind, Variadic Kinds is there and way more interesting: https://github.com/Microsoft/TypeScript/issues/5453
A few questions for TS veterans:
- With babel, we used the module-resolver plugin to set a custom root for absolute imports. In TS, it seems relative imports (import x from '../../../i-hate-life') are the norm, which turns ugly fast. How are folks dealing with this? Or aren’t they?
- Our codebase uses Ramda heavily for FP, and it can be hard to get TS to play nicely sometimes. Any tips on how to most effectively do Ramda/FP in TS?
- Am I missing out by not switching to VSCode? I’m using ST3 currently and it works, but there’s no fancy intellisense. Are the benefits worth picking up a new editor for?
I would say yes! VSCode also has many other things to offer over ST3 besides IntelliSense btw.
* Git integration: You can directly edit in diffs, commit from the sidebar, see your submodules and commit in them, ...
* Integrated terminal which can also run tasks, so colors and input will work
* Much better debugging support (AFAIK ST3 has none out-of-the-box)
It's been a while since I've seriously used Sublime Text though.
https://www.typescriptlang.org/docs/handbook/module-resoluti...
- https://github.com/dividab/tsconfig-paths - resolves paths aliases at runtime in Node
- https://github.com/duffman/tspath - run after build, it rewrites the imports in the resulting JavaScript files
What's nice about Ramda is that while it uses the Haskell-ish vocabulary and type signatures in its docs, someone can also just dive in and start using it as if it were an underscore-like library and not worry about all the more cerebral stuff.
See: https://decembersoft.com/posts/say-goodbye-to-relative-paths...
Here's an example:
// tsconfig.json
...
"paths": {
"#src/*": ["src/*"],
}
...
// package.json
...
"requireRewrite": {
"map": [
[
"#src/",
"dist/src/"
],
]
}
...
// src/path1/x.ts
import { X } from '#src/path2/y.ts`Well... typescript comes with tsserver, which is a local language-server (a Typescript LSP-server), which is what VSCode uses to provide all that goodness.
If you find a LSP-plugin for your favorite editor, it should thereotically be able to offer the same.
For instance, Emacs has tide[1] which uses tsserver (just like VSCode) to provide the same functionality.
I’d be surprised if there were no LSP or tsserver-clients for ST3. No need to leave something you know well over a single feature it may actually support.
The tuple improvements should be quite exciting for many FP/FP-ish libraries in TS.
npm i express @types/express
It usually "just works", but it really depends on how obscure the packages you're using are.4,720 type definition files at the moment: https://github.com/DefinitelyTyped/DefinitelyTyped/tree/mast...
Others have types available in "@types/<package>".
For everything else, I create a "types.d.ts" file that just has a bunch of stuff like:
declare module 'twilio';
declare module 'bitly';
declare module 'cloudinary';
declare module 'tmp-promise';
declare module 'dialogflow';
declare module 'fuel-rest';
And then those modules get set to the "any" type.
These are all nitpicks in light of the amazing achievement that is typechecking JavaScript. But I wouldn't mind a little more ergonomics for package typing.
Writing a temporary definition file to just make something work is as easy as a one-liner in most cases now (make a .d.ts file somewhere in your project and add: `declare module 'module-name'` and/or `declare module 'module-name/*'`, depending on how the module is supposed to be used).
> Also, why do I need to clone the DefinitelyTyped repo and make a PR to add a typing file? Why can't I just make an NPM package?
It is possible. DefinitelyTyped is a publishing infrastructure bottleneck for sure. But it's a useful infrastructure bottleneck in doing somewhat minimal gatekeeping of the `@types` namespace on NPM and keeping it from filling with garbage. (I was a somewhat vocal critic of it before NPM @types, but I've mellowed again on it, partly because it is shrinking slowly in some areas as more NPM libraries adopt types directly. Some of my favorite contributions are PRs to remove types in DT because a library added their own directly to their NPM packages.)
Though a baked-in default in Typescript, @types is only semi-magic. The configuration knob is called typeRoots [1]. You can't configure a recursive search path like `@types` with it, but you can use it for experimenting with non-DefinitelyTyped types packages. (DT itself uses typeRoots internally for all of its testing.)
[1] https://www.typescriptlang.org/docs/handbook/tsconfig-json.h...
Hardly a problem at all.
A bit like Objective-C and C++ got free of their initial compilation model.
Not only TypeScript, one day we would be able to compile Haskell as well: https://github.com/WebGHC
A much better model would be a progressive enhancement, where we can deliver optional types that browsers can then use if they understand them. That way there will also be a reason to upgrade for users.
The only reason TS is able to iterate so quickly is because of the compilation cycle. Without it, progress forward would probably come to a screeching halt.
Param destructuring is one of the absolute best features of ES2015. TypeScript breaks it.
Reading this page I can't actually tell if param destructuring is even being addressed but I can see alot of at first glance weird and obtuse looking code.
Not an obvious up front win.
Wait, Typescript didn't have async/await and generators until this release???!!!
There’s basically no extra effort to using typescript if you already have a build pipeline running. It’s not like using Java or Haskell where you’re drowning in type definitions.
TypeScript makes writing frontends easier and it makes writing Node-JS apps easier. It makes it easier to both read and write code.
> Just didn't feel worth the effort if you're building for throwawayability.
Now that's a legitimate point. If you're building for throwawayability, then TypeScript and FlowType are probably not worth your effort. For other people, modeling things with types helps them move faster even for throwawayability.
Personally, I tend to code first, use type inferencing first, and then annotate later. However, I also tend to use a lot of reflection as well to help me prototype things quicker. If I've found the types I've wanted after I've explored the domain space, I'll then annotate afterward to lock them in.
As with anything, YMMV.
If you've ever had to debug a misbehaving Knockout template just to find it was referencing a misspelled object property, you can appreciate how much cleaner TSX templating is.