8 ms·
JavaScript for squares: The incredible rise of TypeScript
- valuearb 9y agoI spend most of my time writing in Swift, The rest of the time I'm JavaScript, talk about a schism. I greatly prefer Swift, which is why I'm installing Typescript this week. Maybe because I'm less experienced with JS, but I make too many silly mistakes in it. I know a lot of people love the freedom of JavaScript, but I also know lots of people write crappy code.
- nojvek 9y agoSwift and Typescript go really well together. Typescript made me get into swift and now into my first app in appstore.
- brian_herman 9y agojavascript... i like it because it is the wrong way to do it and it feels so good.
- slackingoff2017 9y agoLike turning left on a red when nobody's around
- zumu 9y agoLegal in Oregon if it's on to a one way
- Waterluvian 9y agoLegal in Canada too. I just assumed it's legal everywhere because logically it's a safe operation. My favourite is a one way /one way intersection in my town where I will turn left from the far left lane into the far left lane. It feels very British. Oh no this is really far off topic. I'm a JS developer in robotics and I keep getting tempted by the allure of TS. The only reason I don't take the plunge is that I'm the only developer and mitigate most typing issues because I'm so intimate with the code. I suspect as a team grows I'll regret not going TS :)
- nailer 9y agoI'm not a 'Typing is Safety' person - languages I use tend to be strongly, dynamically typed. At least one study has shown more errors come from other issues other than type mismatches and that's also my own experience. That said: as my ES2017 app gets more complex, a little type safety could be useful. I think it might catch more stuff - eg, earlier this week I tried to use a Promise (because I forgot an 'await') and then got a bunch of NaNs on invoices (the unit tests had the await, so passed, the route didn't). Renaming something to .ts and adding a quick compilation step doesn't seem like a big deal and would catch that error. Typescript also has a MASSIVE amount of community support around it. So yeah: still don't believe 'typing is safety' but will likely end up using TypeScript by the end of the year.
- slackingoff2017 9y agoIMO the static analysis that becomes possible from using types is more useful than types themselves. My favorite JS gotcha is the number of special values a variable can take on. NaN, Undefined, null, Infinity, -Infinity, maybe more? Also the fact that arrays and objects are always truthy. Honorable mention for the seemingly random results of concatenating strings. I'm tired of getting burned by these design flaws and if it takes strong typing to be protected from them than so be it.
- draw_down 9y agoI came to JS after spending years in C# and Java, and so I liked the dynamically-typed nature of the language. It feels a lot freer, to me. Of course I understand why people think static typing is important, but going back to that way of denoting what everything is just seems so onerous. But even regardless of that, what I really hate is how these typed JS variants introduce so much extra syntax and -- sorry for my phraseology -- crap into the language. Here is an example of Flow: declare function isMap(m: mixed): boolean %checks(m instanceof Map); You can call that whatever you want, but JavaScript it ain't.
- shakna 9y agoType deduction isn't a terrible hardship, and should make the code look dynamic. A simple debugging output can attach the types visibly. So... My question is why these statically typed JSs add such verbose syntax? Even C++ has had auto to avoid irritating boilerplate.
- girvo 9y agoFlow doesn't require you to write what's above at all for the most part, and it's type inference and flow analysis is amazing. Your applications code will have few type hints if that's what you want, and your core libraries can have the extremely specific types as what's show in the parent post. I dunno, I like TS and Flow, and gradual typing with excellent inference means it rarely gets in my way, even when calling out to JS libraries that are as dynamic and loose as you can get.
- girvo 9y agoSure, but that isn't JavaScript and isn't needed at runtime anyway. It's literally stripped out, not even compiled (unlike TS)! Flows type system is amazingly powerful, too, which I adore. So I'm biased; being able to emulate HKTs with real type checking gives me the dynamic power of JS with the type safety I want; and it's an easier sell than Purescript ;)
- brylie 9y ago> incremental typechecking sounds small but isn’t. It pays huge dividends in developer productivity. As Dale notes, “Refactors that used to take weeks take days, sometimes less..." Just out of curiosity, what is the basis of claims like that? What types of bugs does static typing prevent, and how pervasive are those bugs otherwise?
- slackingoff2017 9y agoI encourage you to convert some of your JS code. In my experience converting something like 10k lines, this is absolutely true. I convert using strictest compile rules + TsLint with most rules on. In those 10kish lines I found hundreds of potential bugs. I'm sure at least a third were real. The biggest culprits in our codebase were classic type errors and improper use of truthy/falsey. Adding types enables a ton of static analysis like identification of broken conditional statements. I found over 100 if statements that were always true/false or otherwise ineffective.
- tonyedgecombe 9y ago"I found over 100 if statements that were always true/false or otherwise ineffective." I'm not sure whether that shows how good Typescript is or how bad Javascript is.
- slackingoff2017 9y agoMostly the latter. With strict type checking enabled TsLint will warn you about impossible conditionals. You can go a step farther and force conditionals to return boolean true/false. This disables JavaScript's "truth/falsey" but prevents a whole class of errors caused by questionable design decisions... Like empty arrays being truthy.
- badlucklottery 9y agoFrom a refactoring perspective, typechecking makes the answer to the question "What is this change going to break?" much more obvious. Combine that with Typescript's IDE support and a lot of the usual dumb refactoring mistakes just don't happen so you can focus on the actual algorithmic problems emerging.
- nonsince 9y ago> As Tom Dale, a senior software engineer at LinkedIn and a JavaScript luminary, more colorfully described it to me, “JavaScript is like the hippie parents who let you have premarital sex and smoke weed.” That sounds great, he notes, even liberating, but “eventually you resent that you didn’t have any structure and now you’re 32 and living in their basement.” I don't mean to imply anything about this man's outlook on life from these quotes, but that's a mormon-worthy stereotyping of premarital sex and weed.
- mcappleton 9y agoThe thing I like most about TS is interfaces. I use a single "options" parameter for functions whenever there are a bunch of parameters, and having autocomplete for it makes life so much easier.